Of course! The mistake is that I didn't have the ID in the URL, but I took the partner to be edited somewhere from the session. I'll change the code and it should work as expected.
Thanks again for your patience Marc On Jul 21, 10:57 pm, Daniel Roseman <dan...@roseman.org.uk> wrote: > On Jul 21, 9:34 pm, mettwoch <mettw...@pt.lu> wrote: > > > > > I'd like this to work: > > > class PartnerForm(forms.ModelForm): > > class Meta: > > model = Partner # I expect that all fields are included in > > the form (the 'id' field as well) > > > def partner_new(request): > > form = PartnerForm() > > return render_to_response('pos.html', {'form': form}, > > RequestContext(request)) > > > def partner_edit(request): > > form = PartnerForm(instance = request.session['partner']) > > return render_to_response('pos.html', {'form': form}, > > RequestContext(request)) > > > def partner_save(request): > > if request.POST.get('id', None): > > form = PartnerForm(request.POST, instance = get_object_or_404 > > (Partner, pk = request.POST['id'])) > > else: > > form = PartnerForm(request.POST) > > if form.is_valid(): > > form.save() > > return render_to_response('pos.html', {}, RequestContext > > (request)) > > else: > > return render_to_response('pos.html', {'form': form}, > > RequestContext(request)) > > > here's an excerpt of the pos.html: > > > {% if form %} > > <form method="POST", action="{% url partner_save %}"> > > <table> > > {{ form.as_table }} > > </table> > > <input type="submit" value="save" /> > > </form> > > ... > > > as I said: maybe I'm on the wrong track with ModelForm ... > > Well, yes. What you've posted doesn't make any sense at all. You want > to get the id from the form, so that you can get the instance from the > database to pass to the form so it can get the id! This is obviously > circular. At some point, you've got to tell the form what the id > actually is, and that means passing it in to the page where you first > render the form. If you don't have an id, you pass a blank instance, > and a new row will be made in the database. If you do have one, pass > the instance with that id, and the existing row will be edited. > > Various people have posted more or less complex code to do this, but > the simplest is like this: > > def my_form_view(request, partner_id=None): > if partner_id: > partner = Partner.objects.get(id=partner_id) > else: > partner = Partner() > > if request.POST: > form = PartnerForm(request.POST, instance=partner) > if form.is_valid(): > form.save() > return HttpResponseRedirect('/') > else: > form = PartnerForm(instance=partner) > > return render_to_response('pos.html', {'form': form}, > RequestContext(request)) > > One final thing - make your form action="." so it posts back to the > same URL, ie the one with the partner_id. > > That's all there is to it - nothing complicated at all. > -- > 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 -~----------~----~----~----~------~----~------~--~---