手动示例:http://docs.djangoproject.com/en/1.0/topics/forms/formsets/#formset-validation(我使用 Django 1.0.3 在 Google App Engine 上运行)
代码:
from django import forms
from django.forms.formsets import formset_factory
class ArticleForm1(forms.Form):
title = forms.CharField()
pub_date = forms.DateField()
class ArticleForm2(forms.Form):
title = forms.CharField()
class ArticleForm3(forms.Form):
title = forms.CharField()
pub_date = forms.CharField()
ArticleFormSet1 = formset_factory(ArticleForm1)
ArticleFormSet2 = formset_factory(ArticleForm2)
ArticleFormSet3 = formset_factory(ArticleForm3)
data = {
'form-TOTAL_FORMS': u'2',
'form-INITIAL_FORMS': u'0',
'form-0-title': u'',
'form-0-pub_date': u'16 June 1904',
'form-1-title': u'', # <-- this title is missing but required
'form-1-pub_date': u'16 June 1904', # <-- this date is missing but required
}
formset = ArticleFormSet1(data)
print "Should be False: %s" % formset.is_valid()
formset = ArticleFormSet2(data)
print "Should be False: %s" % formset.is_valid()
formset = ArticleFormSet3(data)
print "Should be False: %s" % formset.is_valid()
输出:
$ .../ActiveStatePython2.5/python.exe formset_bug.py
Should be False: False
Should be False: True
Should be False: False
ActiveState Python 2.5.4.4,Django 1.0.3 最终版。
它看起来好像在只有一个 CharField 的情况下没有验证(但是 2 个 CharFields 或一个 CharField 和一个 DateField 有效)。
我用谷歌搜索了这样一个错误,但找不到任何东西。我还没有尝试过 Django 1.1,但现在在 GAE 上使用 1.0 要容易得多。