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(r开发者_如何转开发equest) )
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??
I think you should initialize the form like so (as you have already but commented out).
form = EventForm(post_data, request.FILES, instance=event)
Then the event object is associated with the form, and form.save()
can perform update instead of insert.
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.
Profile show and update working code:
@login_required(login_url=LOGIN_URL)
@csrf_exempt
def profile(request):
"""
:param request:
:param:
:return:
"""
val = get_object_or_404(Profil, user=request.user)
if request.method == 'POST':
form = ProfilForm(request.POST, request.FILES, instance=val)
if form.is_valid():
form.save()
msg_ok = u'Profil updated'
else:
msg_err = u"Invalid form."
else:
form = ProfilForm(instance=val)
return render(request, "back/profile.html", locals())
You example is too complex. Try to start from scratch with simple model/view/form. Create new model with one simple field, then create model form and view and ensure that they works. Then start modifying your view to get it closer to the actual state.
精彩评论