Can I use multiple number of formset in a single form in django,if yes how?

末鹿安然 提交于 2019-12-06 02:06:37

问题


I have to make a form in which more than one formset is used. please tell me if this is possible. if yes then how?


回答1:


You can add as many formsets in the form. Just create/init them in view and pass to template to render in the form.

Something like:

{{ formset1.management_form }}
{% for form in formset1 %}
    {{ form }}
{% endfor %}

{{ formset2.management_form }}
{% for form in formset2 %}
    {{ form }}
{% endfor %}

You are using multiple formsets in one view, you need to use prefix for the forms as explained here Using more than one formset in a view In short:

article_formset = ArticleFormSet(prefix='articles')
book_formset = BookFormSet(prefix='books')



回答2:


When adding a prefix='article' We need to consider that the id of the formset will change and there are other changes too like

<label for="id_form-0-title">Title:</label> 

<label for="id_article-0-title">Title:</label>


来源:https://stackoverflow.com/questions/11324723/can-i-use-multiple-number-of-formset-in-a-single-form-in-django-if-yes-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!