I am trying to make a view that allows the user to edit an instance of a model (event in this case). Unfortunately, submitting this form creates a new instance (with a new identifier) ββand does not even delete the old instance. I get the impression that the save method should update the instance in this case ...
NOTE. EventForm is ModelForm
I tried using force_update arg per https://docs.djangoproject.com/en/dev/ref/models/instances/#forcing-an-insert-or-update , but not a cube. I also tried just deleting the original event in the form.is_valid() block (by calling event.delete() ), but ... without a bone.
Do I have the feeling that the commit=False problem? I'm not sure!
Thanks.
(Please ignore the interval problems in the 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 can get rid of the line form.save_m2m() . This still does not work.
I also tried not to submit the instance when submitting the form, on the assumption that the correct fields will be pre-populated when the user submits (which is the case right now). This still does not work.
Am I missing an important detail when it comes to updating the model?
source share