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).
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?
If so, I know the pk value for the institution at the time I CREATE
the form (with form_for_model). I'm converting the widget for that
field to HIDDEN_INPUT so the user doesn't see it. But I haven't been
successful in setting the value of that hidden input field at form-
create time -- if I could do that it might solve my problem on the
backend when I get the form data POSTed.
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?
Failing that, can anyone point me to a good tutorial on the old forms
package?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---