Tuesday, September 28, 2010

Formsets, forms and related editing

Hello again,

after some trying and not coming to a successful end I might need a hint or two with the following construction.

Assume a pilots flight log: Each flight can have several legs, and a purpose. So basically the model looks like this:

Mission:
    Pilot
    Purpose
    ...

Leg:
    Mission: ForeignKey('mission')
    Departure
    Target
    Time

The relationship is Mission (1) --> (*) Leg (one to many). The legs are REQUIRED for the Mission to be complete.

Now I am trying to design a view which presents an input for the Mission AND the leg part, so when submitting you create a Mission including its legs. There are also several restrictions, one of them being that a pilot can only log his own Missions. For this reason I do not present a "Pilot" entry field, and intend to fill in the Pilot later (it's of course a registered user, so I am following the procedure described in the Django docs - save(commit=False), update the field value).

But this does not work. :/

The view is roughly this code:
    mission = MissionForm()  # a modelform
    LegFormSet = inlineformset_factory(Mission, Leg, form=LegForm, extra=1, can_delete=False)
    legforms = LegFormSet(prefix="legformset")
    render_to_response("...", { ... }, context_instance=...)

The View seems to work all right, but the processing does not. The processing code is roughly:
    mission = MissionForm(request.POST).save(commit=False)
    # it seems to abort here with the error "The Mission could not be created because the data didn't validate"
    legs =[f.save(commit=False) for f in LegFormSet(request.POST, prefix="legformset")]

Are there any best practices when doing such kind of thing? Am I doing it wrong?


thanks in advance for any help & greetings,
Axel.

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment