Formsets 阅读笔记

来源:互联网 发布:win10软件乱码 编辑:程序博客网 时间:2024/06/06 00:06

Formsets

★ 创建 Form 的集合 

class django.forms.formsets.BaseFormSet

A formset is a layer of abstraction to work with multiple forms on the same page. It can be best compared to a data grid. Let’s say you have the following form: 

 ★ formset 可以在同一个页面显示多个 form ,好比为一个数据“网格”。例如,你有一个 ArticleForm 

>>> from django import forms>>> class ArticleForm(forms.Form):...     title = forms.CharField()...     pub_date = forms.DateField()

You might want to allow the user to create several articles at once. To create a formset out of an ArticleForm you would do:  

★ 想一次创建多个 article。用 formset_factory 创建 ArticleForm 的 formset 

>>> from django.forms.formsets import formset_factory>>> ArticleFormSet = formset_factory(ArticleForm)

You now have created a formset named ArticleFormSet. The formset gives you the ability to iterate over the forms in the formset and display them as you would with a regular form: 

 ★ 现在创建了一个名为 ArticleFormSet 的 formset,这个 formset 能够循环显示多个 form 

>>> formset = ArticleFormSet()>>> for form in formset:...     print(form.as_table())<tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" id="id_form-0-title" /></td></tr><tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" id="id_form-0-pub_date" /></td></tr>

As you can see it only displayed one empty form. The number of empty forms that is displayed is controlled by the extra parameter. By default, formset_factory() defines one extra form; the following example will display two blank forms:  

★ 你可以看到这个 formset 只生成了一个空的 form,可以用参数 extra 来控制生成 form 的数量。默认地,formset_factory() 只生成一个 form;下面的例子会显示两个空 form 

>>> ArticleFormSet = formset_factory(ArticleForm, extra=2)

Iterating over the formset will render the forms in the order they were created. You can change this order by providing an alternate implementation for the __iter__() method.  

★ 循环这个 formset 会按照它们创建的顺序显示 forms。可以实现一个 __iter__() 方法来改变它们的顺序 

Formsets can also be indexed into, which returns the corresponding form. If you override __iter__, you will need to also override __getitem__ to have matching behavior.  ★ TODO 

Using initial data with a formset

Initial data is what drives the main usability of a formset. As shown above you can define the number of extra forms. What this means is that you are telling the formset how many additional forms to show in addition to the number of forms it generates from the initial data. Lets take a look at an example:  ★ 可以用数据初始化一个 formset 

>>> import datetime>>> from django.forms.formsets import formset_factory>>> from myapp.forms import ArticleForm>>> ArticleFormSet = formset_factory(ArticleForm, extra=2)>>> formset = ArticleFormSet(initial=[...     {'title': u'Django is now open source',...      'pub_date': datetime.date.today(),}... ])>>> for form in formset:...     print(form.as_table())<tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" value="Django is now open source" id="id_form-0-title" /></td></tr><tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" value="2008-05-12" id="id_form-0-pub_date" /></td></tr><tr><th><label for="id_form-1-title">Title:</label></th><td><input type="text" name="form-1-title" id="id_form-1-title" /></td></tr><tr><th><label for="id_form-1-pub_date">Pub date:</label></th><td><input type="text" name="form-1-pub_date" id="id_form-1-pub_date" /></td></tr><tr><th><label for="id_form-2-title">Title:</label></th><td><input type="text" name="form-2-title" id="id_form-2-title" /></td></tr><tr><th><label for="id_form-2-pub_date">Pub date:</label></th><td><input type="text" name="form-2-pub_date" id="id_form-2-pub_date" /></td></tr>

There are now a total of three forms showing above. One for the initial data that was passed in and two extra forms. Also note that we are passing in a list of dictionaries as the initial data.   ★ 传的是一个字典列表 

See also

Creating formsets from models with model formsets.

Limiting the maximum number of forms

The max_num parameter to formset_factory() gives you the ability to limit the maximum number of empty forms the formset will display: ★ 用 max_num 参数可以限制 formset_factory() 的最大数量

>>> from django.forms.formsets import formset_factory>>> from myapp.forms import ArticleForm>>> ArticleFormSet = formset_factory(ArticleForm, extra=2, max_num=1)>>> formset = ArticleFormSet()>>> for form in formset:...     print(form.as_table())<tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" id="id_form-0-title" /></td></tr><tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" id="id_form-0-pub_date" /></td></tr>

If the value of max_num is greater than the number of existing objects, up to extra additional blank forms will be added to the formset, so long as the total number of forms does not exceed max_num.  ★ 如果 max_num 超过了已存的 extra 数目,生成的 form 个数会是 extra 的个数,只要 extra 不超过 max_num 

max_num value of None (the default) puts a high limit on the number of forms displayed (1000). In practice this is equivalent to no limit.

★ 若 max_num=None 则 forms 的数量为1000,实际这相当于没有限制数量(毕竟1000个form用不上)。

If the number of forms in the initial data exceeds max_num, all initial data forms will be displayed regardless. (No extra forms will be displayed.)

★ 若初始化数据的数量超过了 max_num,所有的初始化数据都会被显示 

By default, max_num only affects how many forms are displayed and does not affect validation. If validate_max=True is passed to the formset_factory(), then max_num will affect validation. See Validating the number of forms in a formset.

★ 默认地,max_num 仅仅影响多少个 form 被显示,并不影响 validation。如果 validation=True 作为 formset_factory() 的参数,那么 max_num 会影响 validation。参考 Validating the number of forms in a formset 

Changed in Django 1.6:

The validate_max parameter was added to formset_factory(). Also, the behavior of FormSet was brought in line with that of ModelFormSet so that it displays initial data regardless of max_num.

Formset validation

Validation with a formset is almost identical to a regular Form. There is an is_valid method on the formset to provide a convenient way to validate all forms in the formset:  ★ 用 is_valid 方法可以很方便验证 forms 

>>> from django.forms.formsets import formset_factory>>> from myapp.forms import ArticleForm>>> ArticleFormSet = formset_factory(ArticleForm)>>> data = {...     'form-TOTAL_FORMS': u'1',...     'form-INITIAL_FORMS': u'0',...     'form-MAX_NUM_FORMS': u'',... }>>> formset = ArticleFormSet(data)>>> formset.is_valid()True

We passed in no data to the formset which is resulting in a valid form. The formset is smart enough to ignore extra forms that were not changed. If we provide an invalid article:

>>> data = {...     'form-TOTAL_FORMS': u'2',...     'form-INITIAL_FORMS': u'0',...     'form-MAX_NUM_FORMS': u'',...     'form-0-title': u'Test',...     'form-0-pub_date': u'1904-06-16',...     'form-1-title': u'Test',...     'form-1-pub_date': u'', # <-- this date is missing but required... }>>> formset = ArticleFormSet(data)>>> formset.is_valid()False>>> formset.errors[{}, {'pub_date': [u'This field is required.']}]

As we can see, formset.errors is a list whose entries correspond to the forms in the formset. Validation was performed for each of the two forms, and the expected error message appears for the second item.

total_error_count()
New in Django 1.6.

To check how many errors there are in the formset, we can use the total_error_count method:  ★ 检查有多少个 error,用 total_error_count 方法 

>>> # Using the previous example>>> formset.errors[{}, {'pub_date': [u'This field is required.']}]>>> len(formset.errors)2>>> formset.total_error_count()1

We can also check if form data differs from the initial data (i.e. the form was sent without any data):  ★ 可以检查 form 数据是否和初始化数据一样 

>>> data = {...     'form-TOTAL_FORMS': u'1',...     'form-INITIAL_FORMS': u'0',...     'form-MAX_NUM_FORMS': u'',...     'form-0-title': u'',...     'form-0-pub_date': u'',... }>>> formset = ArticleFormSet(data)>>> formset.has_changed()False

Understanding the ManagementForm

You may have noticed the additional data (form-TOTAL_FORMSform-INITIAL_FORMS and form-MAX_NUM_FORMS) that was required in the formset’s data above. This data is required for the ManagementForm. This form is used by the formset to manage the collection of forms contained in the formset. If you don’t provide this management data, an exception will be raised:  ★ form-TOTAL_FORMS, form-INITIAL_FORMS 和 form-MAX_NUM_FOMRS 一些 formset 数据是 ManagementForm 需要的。这个 form 被 formset 使用以管理在 formset 里的集合。你不需要提供这个管理 form 的数据。

>>> data = {...     'form-0-title': u'Test',...     'form-0-pub_date': u'',... }>>> formset = ArticleFormSet(data)Traceback (most recent call last):...django.forms.util.ValidationError: [u'ManagementForm data is missing or has been tampered with']

It is used to keep track of how many form instances are being displayed. If you are adding new forms via JavaScript, you should increment the count fields in this form as well. On the other hand, if you are using JavaScript to allow deletion of existing objects, then you need to ensure the ones being removed are properly marked for deletion by including form-#-DELETE in the POST data. It is expected that all forms are present in the POST data regardless.  ★ ManagementForm 被用来追踪多少个被显示的form实例。因此,当用 Javascript 追加新的 form时,应该增加 ManagementForm 的数量;另一方面,当使用 Javascript 来删除已存的对象时,需要确保被删除的对象在 POST 数据里被 form-#-DELETE 标记

The management form is available as an attribute of the formset itself. When rendering a formset in a template, you can include all the management data by rendering {{ my_formset.management_form }} (substituting the name of your formset as appropriate).

★ management form 作为 formset 本身的属性。当在模板里渲染 formset 时,用 {{ my_formset.management_form }} 来获得所有的 management 数据。

total_form_count and initial_form_count

BaseFormSet has a couple of methods that are closely related to the ManagementFormtotal_form_count and initial_form_count.

total_form_count returns the total number of forms in this formset. initial_form_count returns the number of forms in the formset that were pre-filled, and is also used to determine how many forms are required. You will probably never need to override either of these methods, so please be sure you understand what they do before doing so.

★ BaseFormSet 有一系列的和 ManagementForm, total_form_set, initial_form_count 关联的的方法。total_form_count 返回 formset 的总form数。initial_form_count 返回 formset 里被初始化的form数。你不用覆盖这两个方法,要明白它们的原理。

empty_form

BaseFormSet provides an additional attribute empty_form which returns a form instance with a prefix of __prefix__ for easier use in dynamic forms with JavaScript.

★ BaseFormSet 提供了 empty_form 属性,这个属性返回一个以 __prefix__ 为前缀的 form 实例,这个实例在用 JavaScript 生成动态的 forms 时使用。

Custom formset validation

A formset has a clean method similar to the one on a Form class. This is where you define your own validation that works at the formset level:

★ formset 有一个 clean 方法可以让你定义自己的 validation 

>>> from django.forms.formsets import BaseFormSet>>> from django.forms.formsets import formset_factory>>> from myapp.forms import ArticleForm>>> class BaseArticleFormSet(BaseFormSet):...     def clean(self):...         """Checks that no two articles have the same title."""...         if any(self.errors):...             # Don't bother validating the formset unless each form is valid on its own...             return...         titles = []...         for form in self.forms:...             title = form.cleaned_data['title']...             if title in titles:...                 raise forms.ValidationError("Articles in a set must have distinct titles.")...             titles.append(title)>>> ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet)>>> data = {...     'form-TOTAL_FORMS': u'2',...     'form-INITIAL_FORMS': u'0',...     'form-MAX_NUM_FORMS': u'',...     'form-0-title': u'Test',...     'form-0-pub_date': u'1904-06-16',...     'form-1-title': u'Test',...     'form-1-pub_date': u'1912-06-23',... }>>> formset = ArticleFormSet(data)>>> formset.is_valid()False>>> formset.errors[{}, {}]>>> formset.non_form_errors()[u'Articles in a set must have distinct titles.']

The formset clean method is called after all the Form.clean methods have been called. The errors will be found using the non_form_errors() method on the formset.  ★ formset 的 clean 方法在 Form.clean 方法之后调用,non_form_errors() 方法可以返回 errors 

Validating the number of forms in a formset

If validate_max=True is passed to formset_factory(), validation will also check that the number of forms in the data set, minus those marked for deletion, is less than or equal to max_num.  ★ 如果 formset_facotry() 方法 的 validate_max=True,那么 validation 也会 check form 的数量,减去被删除后的数量小于等于 max_num 

>>> from django.forms.formsets import formset_factory>>> from myapp.forms import ArticleForm>>> ArticleFormSet = formset_factory(ArticleForm, max_num=1, validate_max=True)>>> data = {...     'form-TOTAL_FORMS': u'2',...     'form-INITIAL_FORMS': u'0',...     'form-MAX_NUM_FORMS': u'',...     'form-0-title': u'Test',...     'form-0-pub_date': u'1904-06-16',...     'form-1-title': u'Test 2',...     'form-1-pub_date': u'1912-06-23',... }>>> formset = ArticleFormSet(data)>>> formset.is_valid()False>>> formset.errors[{}, {}]>>> formset.non_form_errors()[u'Please submit 1 or fewer forms.']

validate_max=True validates against max_num strictly even if max_num was exceeded because the amount of initial data supplied was excessive.

Applications which need more customizable validation of the number of forms should use custom formset validation.

Note

Regardless of validate_max, if the number of forms in a data set exceeds max_num by more than 1000, then the form will fail to validate as if validate_max were set, and additionally only the first 1000 forms abovemax_num will be validated. The remainder will be truncated entirely. This is to protect against memory exhaustion attacks using forged POST requests.

Changed in Django 1.6:

The validate_max parameter was added to formset_factory().

Dealing with ordering and deletion of forms

The formset_factory() provides two optional parameters can_order and can_delete to help with ordering of forms in formsets and deletion of forms from a formset.

★ formset_factotry() 方法提供了两个可选的参数,can_order 和 can_delete,以帮助排序和删除

can_order

Default: False

Lets you create a formset with the ability to order:

>>> from django.forms.formsets import formset_factory>>> from myapp.forms import ArticleForm>>> ArticleFormSet = formset_factory(ArticleForm, can_order=True)>>> formset = ArticleFormSet(initial=[...     {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)},...     {'title': u'Article #2', 'pub_date': datetime.date(2008, 5, 11)},... ])>>> for form in formset:...     print(form.as_table())<tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" value="Article #1" id="id_form-0-title" /></td></tr><tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" value="2008-05-10" id="id_form-0-pub_date" /></td></tr><tr><th><label for="id_form-0-ORDER">Order:</label></th><td><input type="number" name="form-0-ORDER" value="1" id="id_form-0-ORDER" /></td></tr><tr><th><label for="id_form-1-title">Title:</label></th><td><input type="text" name="form-1-title" value="Article #2" id="id_form-1-title" /></td></tr><tr><th><label for="id_form-1-pub_date">Pub date:</label></th><td><input type="text" name="form-1-pub_date" value="2008-05-11" id="id_form-1-pub_date" /></td></tr><tr><th><label for="id_form-1-ORDER">Order:</label></th><td><input type="number" name="form-1-ORDER" value="2" id="id_form-1-ORDER" /></td></tr><tr><th><label for="id_form-2-title">Title:</label></th><td><input type="text" name="form-2-title" id="id_form-2-title" /></td></tr><tr><th><label for="id_form-2-pub_date">Pub date:</label></th><td><input type="text" name="form-2-pub_date" id="id_form-2-pub_date" /></td></tr><tr><th><label for="id_form-2-ORDER">Order:</label></th><td><input type="number" name="form-2-ORDER" id="id_form-2-ORDER" /></td></tr>

This adds an additional field to each form. This new field is named ORDER and is an forms.IntegerField. For the forms that came from the initial data it automatically assigned them a numeric value. Let’s look at what will happen when the user changes these values:  ★ can_order=True 给每个 form 增加了额外的 form。新的 field 被命名为 ORDER,类型为 forms.IntegerField,自动初始化为数字。

>>> data = {...     'form-TOTAL_FORMS': u'3',...     'form-INITIAL_FORMS': u'2',...     'form-MAX_NUM_FORMS': u'',...     'form-0-title': u'Article #1',...     'form-0-pub_date': u'2008-05-10',...     'form-0-ORDER': u'2',...     'form-1-title': u'Article #2',...     'form-1-pub_date': u'2008-05-11',...     'form-1-ORDER': u'1',...     'form-2-title': u'Article #3',...     'form-2-pub_date': u'2008-05-01',...     'form-2-ORDER': u'0',... }>>> formset = ArticleFormSet(data, initial=[...     {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)},...     {'title': u'Article #2', 'pub_date': datetime.date(2008, 5, 11)},... ])>>> formset.is_valid()True>>> for form in formset.ordered_forms:...     print(form.cleaned_data){'pub_date': datetime.date(2008, 5, 1), 'ORDER': 0, 'title': u'Article #3'}{'pub_date': datetime.date(2008, 5, 11), 'ORDER': 1, 'title': u'Article #2'}{'pub_date': datetime.date(2008, 5, 10), 'ORDER': 2, 'title': u'Article #1'}

can_delete

Default: False

Lets you create a formset with the ability to select forms for deletion:

>>> from django.forms.formsets import formset_factory>>> from myapp.forms import ArticleForm>>> ArticleFormSet = formset_factory(ArticleForm, can_delete=True)>>> formset = ArticleFormSet(initial=[...     {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)},...     {'title': u'Article #2', 'pub_date': datetime.date(2008, 5, 11)},... ])>>> for form in formset:....    print(form.as_table())<input type="hidden" name="form-TOTAL_FORMS" value="3" id="id_form-TOTAL_FORMS" /><input type="hidden" name="form-INITIAL_FORMS" value="2" id="id_form-INITIAL_FORMS" /><input type="hidden" name="form-MAX_NUM_FORMS" id="id_form-MAX_NUM_FORMS" /><tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" value="Article #1" id="id_form-0-title" /></td></tr><tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" value="2008-05-10" id="id_form-0-pub_date" /></td></tr><tr><th><label for="id_form-0-DELETE">Delete:</label></th><td><input type="checkbox" name="form-0-DELETE" id="id_form-0-DELETE" /></td></tr><tr><th><label for="id_form-1-title">Title:</label></th><td><input type="text" name="form-1-title" value="Article #2" id="id_form-1-title" /></td></tr><tr><th><label for="id_form-1-pub_date">Pub date:</label></th><td><input type="text" name="form-1-pub_date" value="2008-05-11" id="id_form-1-pub_date" /></td></tr><tr><th><label for="id_form-1-DELETE">Delete:</label></th><td><input type="checkbox" name="form-1-DELETE" id="id_form-1-DELETE" /></td></tr><tr><th><label for="id_form-2-title">Title:</label></th><td><input type="text" name="form-2-title" id="id_form-2-title" /></td></tr><tr><th><label for="id_form-2-pub_date">Pub date:</label></th><td><input type="text" name="form-2-pub_date" id="id_form-2-pub_date" /></td></tr><tr><th><label for="id_form-2-DELETE">Delete:</label></th><td><input type="checkbox" name="form-2-DELETE" id="id_form-2-DELETE" /></td></tr>

Similar to can_delete this adds a new field to each form named DELETE and is a forms.BooleanField. When data comes through marking any of the delete fields you can access them with deleted_forms:  ★ can_delete 为每个 form 增加了名为 DELETE 的新 field,类型为 forms.BooleanField。

>>> data = {...     'form-TOTAL_FORMS': u'3',...     'form-INITIAL_FORMS': u'2',...     'form-MAX_NUM_FORMS': u'',...     'form-0-title': u'Article #1',...     'form-0-pub_date': u'2008-05-10',...     'form-0-DELETE': u'on',...     'form-1-title': u'Article #2',...     'form-1-pub_date': u'2008-05-11',...     'form-1-DELETE': u'',...     'form-2-title': u'',...     'form-2-pub_date': u'',...     'form-2-DELETE': u'',... }>>> formset = ArticleFormSet(data, initial=[...     {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)},...     {'title': u'Article #2', 'pub_date': datetime.date(2008, 5, 11)},... ])>>> [form.cleaned_data for form in formset.deleted_forms][{'DELETE': True, 'pub_date': datetime.date(2008, 5, 10), 'title': u'Article #1'}]

If you are using a ModelFormSet, model instances for deleted forms will be deleted when you call formset.save(). On the other hand, if you are using a plain FormSet, it’s up to you to handle formset.deleted_forms, perhaps in your formset’s save() method, as there’s no general notion of what it means to delete a form.

Adding additional fields to a formset

If you need to add additional fields to the formset this can be easily accomplished. The formset base class provides an add_fields method. You can simply override this method to add your own fields or even redefine the default fields/attributes of the order and deletion fields:  ★ 可以覆盖 add_fields 方法来给 formset 增加额外的 field 

>>> from django.forms.formsets import BaseFormSet>>> from django.forms.formsets import formset_factory>>> from myapp.forms import ArticleForm>>> class BaseArticleFormSet(BaseFormSet):...     def add_fields(self, form, index):...         super(BaseArticleFormSet, self).add_fields(form, index)...         form.fields["my_field"] = forms.CharField()>>> ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet)>>> formset = ArticleFormSet()>>> for form in formset:...     print(form.as_table())<tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" id="id_form-0-title" /></td></tr><tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" id="id_form-0-pub_date" /></td></tr><tr><th><label for="id_form-0-my_field">My field:</label></th><td><input type="text" name="form-0-my_field" id="id_form-0-my_field" /></td></tr>

Using a formset in views and templates

Using a formset inside a view is as easy as using a regular Form class. The only thing you will want to be aware of is making sure to use the management form inside the template. Let’s look at a sample view:  ★ 使用 formset 就像使用一般的 Form 类一样简单 

from django.forms.formsets import formset_factoryfrom django.shortcuts import render_to_responsefrom myapp.forms import ArticleFormdef manage_articles(request):    ArticleFormSet = formset_factory(ArticleForm)    if request.method == 'POST':        formset = ArticleFormSet(request.POST, request.FILES)        if formset.is_valid():            # do something with the formset.cleaned_data            pass    else:        formset = ArticleFormSet()    return render_to_response('manage_articles.html', {'formset': formset})

The manage_articles.html template might look like this:

<form method="post" action="">    {{ formset.management_form }}    <table>        {% for form in formset %}        {{ form }}        {% endfor %}    </table></form>

However the above can be slightly shortcutted and let the formset itself deal with the management form:

<form method="post" action="">    <table>        {{ formset }}    </table></form>

The above ends up calling the as_table method on the formset class.

Manually rendered can_delete and can_order

If you manually render fields in the template, you can render can_delete parameter with {{ form.DELETE }}:  ★ 如果想再模板中手动设置 can_delete,可以使用 {{ form.DELETE }}

<form method="post" action="">    {{ formset.management_form }}    {% for form in formset %}        {{ form.id }}        <ul>            <li>{{ form.title }}</li>            {% if formset.can_delete %}                <li>{{ form.DELETE }}</li>            {% endif %}        </ul>    {% endfor %}</form>

Similarly, if the formset has the ability to order (can_order=True), it is possible to render it with {{ form.ORDER }}.

Using more than one formset in a view

You are able to use more than one formset in a view if you like. Formsets borrow much of its behavior from forms. With that said you are able to use prefix to prefix formset form field names with a given value to allow more than one formset to be sent to a view without name clashing. Lets take a look at how this might be accomplished:  ★ 可以使用多个 formset,使用prefix 可以改变前缀 

from django.forms.formsets import formset_factoryfrom django.shortcuts import render_to_responsefrom myapp.forms import ArticleForm, BookFormdef manage_articles(request):    ArticleFormSet = formset_factory(ArticleForm)    BookFormSet = formset_factory(BookForm)    if request.method == 'POST':        article_formset = ArticleFormSet(request.POST, request.FILES, prefix='articles')        book_formset = BookFormSet(request.POST, request.FILES, prefix='books')        if article_formset.is_valid() and book_formset.is_valid():            # do something with the cleaned_data on the formsets.            pass    else:        article_formset = ArticleFormSet(prefix='articles')        book_formset = BookFormSet(prefix='books')    return render_to_response('manage_articles.html', {        'article_formset': article_formset,        'book_formset': book_formset,    })

You would then render the formsets as normal. It is important to point out that you need to pass prefix on both the POST and non-POST cases so that it is rendered and processed correctly.

0 0
原创粉丝点击