On Mon, 2007-06-04 at 21:22 -0700, larry wrote:
> Complete django novice here.
> 
> I've been able to produce a nice form interface using newforms and
> various examples.  I've run into what I believe may be a common
> problem with saving many-to-many relationships (of which I have quite
> a few on the one form) when the user is submitting a new record.
> 
> I need to validate the form, then supply one missing field value, then
> save the new record.  I use code like this:
> 
>             if client_form.is_valid():
>                 client = client_form.save(commit=False)
>                 client.institution =
> Institution.objects.get(pk=site_id)
>                 client.save()
>                 return HttpResponseRedirect('/myapp/client/%s' %
> (client.id))
> 
> and this works OK, except that all the many-to-many links are lost.
> I've found references to this issue and even a patch in the
> development tree -- although all the patch does is throw an error on
> the save (rather than silently save, losing the m-t-m links).

I'm not sure what "a patch in the development tree" means here. Current
subversion trunk doesn't seem to have anything like that.

There is a ticket in Trac (#4001) that I've been tossing around in my
mind to decide whether it's the right thing to do. That would not lose
the many-to-many inofmration for pre-existing objects. That's the plus
side. The minus side, of course, is that it means there is inconsistent
behaviour in save(), depending on circumstances, so it's not a
no-brainer to apply that patch.

> The same model, when edited in the Admin interface, works fine.  I
> believe the Admin interface uses the old forms package.  Since the
> django documentation warns us off the old forms package (and since
> I've invested a good deal of time in learning the new one) I don't
> want to go back to the old package.
> 
> I'm under the impression that if I could avoid having to set the
> institution field prior to saving then I could just issue a
> client_form.save() in the above code, and that that WOULD save the m-t-
> m information.  Is that correct?

Doesn't look like it from reading the code. A quick experiment on your
end would confirm that, though.

[...]
> Can anyone suggest how I could set a single field value in a form
> after creating it with form_for_model(), and whether that would solve
> my problem with the m-t-m data on the backend?

We need to think through many-to-many saving in the form_for_model()
shortcut case a bit. That's what I'm trying to do when I'm thinking
about #4001.

Remember that the situation where a form maps precisely onto a model is
a very specialised case. In the general case, you need to build the
objects you are going to save manually anyway (since one form provides
information for many models). So the workaround that springs to mind in
this case is to forget about using form_for_model() and just build the
form by hand and construct the save objects in your view. It's only a
few lines of code.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to