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 6023895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:04:26+00:00 2026-05-23T04:04:26+00:00

I am trying to make a view that allows a user to edit a

  • 0

I am trying to make a view that allows a user to edit a model instance (an event in this case). Unfortunately, submitting this form creates a new instance (with a new id) and doesn’t even delete the old instance. I’m under the impression that the save method is supposed to update the instance in this case…

NOTE: EventForm is a ModelForm

I’ve tried using the force_update arg per https://docs.djangoproject.com/en/dev/ref/models/instances/#forcing-an-insert-or-update, but no dice. I’ve also tried just deleting the original event in the form.is_valid() block (by calling event.delete()) but….no dice.

I have a feeling that the commit=False is the issue? I’m not sure!

Thanks.

(Please ignore spacing problems in code snippet)

def edit_event(request, event_id):
 event = Event.objects.get(pk=event_id)  

 if request.method == 'POST':
     post_data = request.POST.copy()

     # here is some validation that can't be done in the ModelForm...

     #form = EventForm(post_data, request.FILES, instance=event)
     form = EventForm(post_data, request.FILES)

     if form.is_valid():

         edited_event = form.save(commit=False)

         edited_event.save(force_update=True) # doesn't work with or without force_update arg

         #form.save_m2m() # needed for ManyToMany relationship

         return HttpResponseRedirect('/events/view/%s' % edited_event.id)
  else:
      form = EventForm(instance=event)

return render_to_response('create_event.html', {'form': form,}, context_instance=RequestContext(request) )

UPDATE

I got rid of the M2M relationship on my model, so I am able to get rid of the form.save_m2m() line. That still doesn’t work.

I also tried not passing the instance when submitting the form, under the assumption that the correct fields will be pre-populated when the user submits (which is the case right now). This still isn’t working.

Am I missing an important detail when it comes to updating a model??

  • 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-05-23T04:04:27+00:00Added an answer on May 23, 2026 at 4:04 am

    From what I can understand, the problem with your code is that you’re always creating a new instance before saving. Since you’ve already obtained the instance of the event in “event = Event.objects.get(pk=event_id)”, you could simply change this instance and save it, and I’m quite sure that would cause django to make an update.

    I advise you to check out this link:
    https://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#how-django-knows-to-update-vs-insert

    It has a pretty simple explanation on the algorithm Django uses to distinguish between inserts and updates. If your object doesn’t have it’s primary key set (probably the “id” field that django creates automatically when you syncdb), then it’ll be treated as a new object. If it has that field set, then Django will try to find that particular record in the database, if it exists, it’ll be updated.

    Hope the answer was clear enough.

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

Sidebar

Related Questions

I am trying to make a search view in Django. It is a search
Trying to make a make generic select control that I can dynamically add elements
I'm trying to make the case for click-once and smart client development but my
I'm trying to make a context menu for a control that is linked to
I am trying to make a div, that when you click it turns into
I am trying to make a JTable that has column spans available. Specifically, I
I'm trying to make an application that uses a UIScrollview and UIPageControl. Ideally, I'd
Im trying to build a page that will allow a user to select a
Trying to make a MySQL-based application support MS SQL, I ran into the following
Trying to make a generic PL/SQL procedure to export data in specific XML format,

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.