Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8267233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:26:39+00:00 2026-06-08T05:26:39+00:00

I am having a problem with my Django code inserting new rows instead of

  • 0

I am having a problem with my Django code inserting new rows instead of updating existing ones. Many people on here have asked for help with a similar problem, but generally they forget about setting instance as in form = SomeModelForm(request.POST or None, instance=some_model_instance). I seem to be doing that right, but it still doesn’t save over existing data.

One other poster had the exact same issue (see Django form INSERTs when I want it to UPDATE). It looks like the code given to him to fix the problem resolved the issue, but no one explained why it worked when he asked!

VIEW

   if form_type == "edit_event":
        # we need to load all the data based on the event_id
        event_id = request.POST['event_id'] or None
        event_to_edit = mapDataManager.getEvent(event_id)
        if event_to_edit and mapDataManager.userCreatedEvent(request.user, event_to_edit):
            # this user actually created this event
            # let them edit it
            event_form = EventModelForm(request.POST or None, instance=event_to_edit)
            location_form = LocationModelForm(request.POST or None, instance=event_to_edit.location)
            list_of_event_scheds = EventSchedule.objects.filter(pk=event_id).values()
            event_sched_formset = EventScheduleModelFormSet(request.POST or None, initial=list_of_event_scheds)

            if event_form and location_form and event_sched_formset:
                if event_form.is_valid() and location_form.is_valid() and event_sched_formset.is_valid():
                    # update event
                    mapDataManager.updateEvent(event_form, location_form, event_sched_formset)
                    # redirect
                    return HttpResponseRedirect(reverse('map'))
                else:
                    # set feedback to show edit event form
                    feedback = "edit_event"

updateEvent()

def updateEvent(self, event_form, location_form, event_sched_forms):

    event = event_form.save(commit=False)
    location = location_form.save()
    event.location = location
    event.save()
    for event_sched_form in event_sched_forms: # loop through formset
        event_sched = event_sched_form.save(commit=False)
        event_sched.event = event
        event_sched.save()

Can anyone help me out? Am I missing something really basic?

EDIT: Just to clarify, NONE of my forms were updating, all were inserting. Based on Daniel’s suggestion I think I need to use formsets to link all three modelforms that are being edited.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:26:42+00:00Added an answer on June 8, 2026 at 5:26 am

    I was able to get the inline model formset working as per Daniel’s suggestion with help from this blog post: http://charlesleifer.com/blog/djangos-inlineformsetfactory-and-you/

    Just as an FYI, one crucial point I was missing is that the formset’s instance refers to the parent model. Also, if you’re displaying the formset manually in your template, don’t forget to include {{ formset.id }}.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having problem with datagrid view. I have attached an image with the code
I'm a django newbie and have been having a problem. In my project root
Hello I seem to be having some problem involving counting in Django. I have
I'm having a problem with a ajax request in a django project. I have
I'm having an odd problem while deploying a Django site using Fabric. I've configured
I'm using django with app-engine-patch and I'm having this wierd problem running manage.py dumpdata
I am having problems with django forms and image uploads. I have googled, read
I've set up django with haystack but im having a few problems. I have
Im having problem in my UIscrollView ,this is what I have done: Whenever a
I have been having a problem where after my mongodb connection to mongohq via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.