Heterogeneous forms in the form of django

I have a specific problem with Django forms that I think should definitely have a solution already written.

I have several different forms that are presented in one view, for example ... (Sorry to just use the pseudocode) ..

class Form1(): #different attributes class Form2() #different attributes 

 <html> <form> {{ 1-instance-Form1 }} {{ 2-instance-Form1 }} {{ 1-instance-Form2 }} {{ 2-instance-Form2 }} </form> </html> 

In addition, I want to give the user the opportunity to add a form instance of one of the form classes available through jquery so that the form can become

 <html> <form> {{ 1-instance-Form1 }} {{ 2-instance-Form1 }} {{ 1-instance-Form2 }} {{ 2-instance-Form2 }} {{ 3-instance-Form2 }} </form> </html> 

Now, when you are looking for a solution to solve such a problem, I came across the concept of Django formset, which, as the documentation describes, is a collection of instances of the same Form class. However, as I see it, forms can be able to handle heterogeneous forms:

With some definitions changed

 class BaseHeterogenousFormSet(StrAndUnicode): def append(form): #add one more form to the formset def is_valid(): #run is_valid for each of the forms in the formset def clean(): #run the clean for each of the forms ... 

Is there something wrong with the way I think about this issue?

+6
source share
1 answer

You can submit multiple forms in the same form, but you need to avoid name clashes ( https://docs.djangoproject.com/en/1.6/topics/forms/formsets/#using-more-than-one-formset- in-a-view )

One formet handles instances of Form1, and the other formet handles instances of Form2.

+5
source

Source: https://habr.com/ru/post/910565/


All Articles