Can I use multiple number of formset in a single f

2020-07-11 05:38发布

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?

2条回答
别忘想泡老子
2楼-- · 2020-07-11 06:07

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>
查看更多
欢心
3楼-- · 2020-07-11 06:27

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')
查看更多
登录 后发表回答