On Oct 26, 5:19 am, robinne <develo...@computer-shoppe.net> wrote: > I am noticing that when I load an existing ModelForm into a template, > and I save (POST), I can't seem to get the primary key back in order > to update that specific record. I am starting to head down the path of > formsets...but that is not what I need. I am updating one record only. > Any help is appreciated! > > VIEW > def UpdateMember(request): > if request.method == "POST": > # I don't seem to have access to primary key... > # I do have the posted data, and can save to database here. > > TEMPLATE (as you can see, I've tried to explicitly include primary > key, both inside and outside the for loop) > {{ Member.MemberID }} > {% for field in Member %} > {{ field.label_tag }} > {{ field }} > {% endfor %} > <input type="button" onclick="document.forms[0].submit()" value="save">
You haven't posted the code that shows how you instantiate the form. Clearly your UpdateMember view must be taking more than just the request parameter - it must include a parameter that gets the object you want to update in the first place. And when you instantiate the form for editing, you must be passing the 'instance' parameter, so you get the data for the form to update. So all you need to do on POST is to use that parameter again. def update_member(request, pk): member = get_object_or_404(member, pk=pk) if request.POST: form = MyForm(instance=member, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/wherever/') else: form = MyForm(instance=member) return render_to_response('mytemplate.html', {'form':form}) -- DR. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---