Hi again,

That's exactly what I did and the point is that it doesn't work! See
my comments below ...

On Jul 21, 8:00 pm, Shawn Milochik <shawn.m...@gmail.com> wrote:
> On Jul 21, 2009, at 12:49 PM, mettwoch wrote:
>
>
>
> > Sorry my old brain doesn't get it. Is there somewhere a complete CRUD
> > example using ModelForm?
>
> > Thanks
> > Marc
>
> CRUD:
>
> If your modelform is called PartnerForm, then.
>
> CREATE and UPDATE:
>
>         #on initial page load (no POST)
>
>         #if you have a partner object already (named partner_instance):
>         partner_form = PartnerForm(instance = partner_instance)
>         #otherwise:
>         partner_form = PartnerForm()
>
>         #in the POST portion of your view
>         partner_form = PartnerForm(request.POST)
>         if partner_form.is_valid():
>                 partner_form.save()

This always creates a new partner! In fact, in the ModelForm
documentation is written that the save() method creates a new instance
unless an existing instance is passed to the ModelForm constructor. So
one has to differentiate if the POST comes from an initially empty
form or from a bound form! How can this be done? I believe that it
could be easily achieved if the primary-key were always included as a
hidden field. If it's empty, then 'create' otherwise 'get' & 'update'.

>
> DELETE:
>         #if you have a partner object named partner_instance
>         partner_instance.delete()
>
> READ:
>         What do you mean?
>         Partner.objects.get(pk = 12) #or whatever

In my specific case the partner is stored in the session and it can be
updated. When a new partner is created it is stored in the session.
There's always an 'actif' partner.

>
>         #or, from the modelform instance, assuming
>         #something like partner_form = PartnerForm(instance = 
> partner_instance)
>         #has happened:
>         if partner_form.is_valid():
>                 partner_name = partner_form.cleaned_data['name']
>
> I hope this helps. If not, please be more specific.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to