Multiple form classes in django generic (class) views

前端 未结 3 1219
广开言路
广开言路 2020-12-01 15:02

I\'d like to use the class based generic views of django 1.3 for forms, but sometimes have to manage multiple form classes in one form. However, it looks like the existing v

3条回答
  •  无人及你
    2020-12-01 15:32

    One of the principles of django is that you can build up one big form out of several smaller forms, with only one submit-button. That's why the

    -tags aren't generated by django itself.

    The problem with generic views, class based or not, and multiple such forms in the background, is of course that the sky's the limit. The forms may be related somehow: a "mother"-form and and optional extra data that depends on the data in the mother (onetoone, say). Then there's the model that is connected to several other models through foreign keys and/or intermediary tables where you'd use form+formsets. Then there's the all-formset kind of page, like in the admin when you make some fields editable directly in the list view. Each of these are different types of multi form views, and I don't think it would be fruitful to make one generic view that would cover all cases.

    If you have a "mother" model though, you can use a standard UpdateView or CreateView and add methods for the extra forms that are called from get() and post(), after the code that deals with the mother model. In form_valid() for instance, if the mother-form is valid you can process the other forms. You'll have the pk of the mother that you then use to connect up the data in the other forms.

提交回复
热议问题