Difficulty with validation of multiple modelforms on 1 page

15 views
Skip to first unread message

Roald

unread,
May 7, 2012, 11:20:41 AM5/7/12
to django...@googlegroups.com
Hi all,

I'm working on an application that uses a HTML-form consisting of multiple Django-ModelForms. Moreover, their validation is interdependent. Moreover, their data is interdependent. Moreover, I use multiple modelforms with the same instance (might be a bad idea anyway). My template looks something like this:

<form ...>
  {% if forms.a %}
    {{ forms.a }}
  {% endif %}
  {{ forms.b }}
  {# and much more of that #}
  ...
</form>

My view looks something like this:

if all([f.is_valid() for k, f in forms.items() if f is not None]):
    if forms['a']:
        a = forms['a'].save()
        a.b = forms['b'].save()
        a.b.c = forms['c'].save()
        a.x = forms['d'].save(commit=false).x
        a.save()
    # and much more such stuff
    ...

Do you have any opinions/pointers on how to do this (most elegantly)? Especially how I can validate one form based on the (cleaned) data of another form? Or how you would  refactor that? Thanks in advance!

Kind regards, Roald
Reply all
Reply to author
Forward
0 new messages