On 7/26/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> Hi group,
>
> I have a rather complex hierarchy of models. Say, I have a model A. A
> has a 1:N relation to model B, which itself has a 1:N relation to
> model C.
>
> What I want is a single web page, on which an instance of A and all
> its related B and C instances can be edited.
>
> What is the best way to achieve this? I am not sure wether it is worth
> the effort of coding something generic like a deepform function.
>
> What are your experiences?
>
> Regards,
> -Justin
It's scarily easy to do this using the "prefix" argument when creating
forms. You might have something like the following:
########
# View #
########
# You would do something similar when handling the POSTed forms,
# just adding an extra data=request.POST argument.
a_form = AForm(prefix='a%' % a.id)
b_forms = []
for b in a.b_set.all():
b_forms.append((BForm(prefix='b%s' % b.id),
[CForm(prefix='c%s' % c.id for c in b.c_set.all()]))
return render_to_response('super_form.html, {
'a_form': a_form,
'b_forms': b_forms,
}, RequestContext(request))
############
# Template #
############
<form...>
<h1>A...</h1>
...
{{ a_form }}
...
(IIRC, you can only unpack tuples with the for tag in the SVN version
- use {% for b_form in b_forms %} along with ) b_form.0 and b_form.1
if you're running 0.96)
{% for b_form,c_forms in b_forms %}
<h2>B...</h2>
...
{{ b_form }}
....
{% for c_form in c_forms %}
<h3>C...</h3>
...
{{ c_form }}
...
{% endfor %}
{% endfor %}
</form>
The POST processing code can get a bit ugly _looking_, but isn't that
complicated - you generally have to loop over all the forms, calling
their is_valid method - if all of them are valid, you do your save
logic, otherwise you do nothing and your forms get redisplayed.
Jonathan.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---