mirror of
https://github.com/django/django.git
synced 2025-08-03 02:23:12 +00:00
Fixed #11418 -- formset.cleaned_data no longer raises AttributeError when is_valid is True. Thanks mlavin!
This also introduces a slightly backwards-incompatible change in FormSet's behavior, see the release docs for details. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14667 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
752bd8bf75
commit
65b380e74a
4 changed files with 93 additions and 13 deletions
|
@ -266,6 +266,36 @@ local flavors:
|
|||
has been removed from the province list in favor of the new
|
||||
official designation "Aceh (ACE)".
|
||||
|
||||
FormSet updates
|
||||
~~~~~~~~~~~~~~~
|
||||
|
||||
In Django 1.3 ``FormSet`` creation behavior is modified slightly. Historically
|
||||
the class didn't make a distinction between not being passed data and being
|
||||
passed empty dictionary. This was inconsistent with behavior in other parts of
|
||||
the framework. Starting with 1.3 if you pass in empty dictionary the
|
||||
``FormSet`` will raise a ``ValidationError``.
|
||||
|
||||
For example with a ``FormSet``::
|
||||
|
||||
>>> class ArticleForm(Form):
|
||||
... title = CharField()
|
||||
... pub_date = DateField()
|
||||
>>> ArticleFormSet = formset_factory(ArticleForm)
|
||||
|
||||
the following code will raise a ``ValidationError``::
|
||||
|
||||
>>> ArticleFormSet({})
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'ManagementForm data is missing or has been tampered with']
|
||||
|
||||
if you need to instantiate an empty ``FormSet``, don't pass in the data or use
|
||||
``None``::
|
||||
|
||||
>>> formset = ArticleFormSet()
|
||||
>>> formset = ArticleFormSet(data=None)
|
||||
|
||||
|
||||
|
||||
.. _deprecated-features-1.3:
|
||||
|
||||
|
|
|
@ -100,7 +100,12 @@ an ``is_valid`` method on the formset to provide a convenient way to validate
|
|||
all forms in the formset::
|
||||
|
||||
>>> ArticleFormSet = formset_factory(ArticleForm)
|
||||
>>> formset = ArticleFormSet({})
|
||||
>>> data = {
|
||||
... 'form-TOTAL_FORMS': u'1',
|
||||
... 'form-INITIAL_FORMS': u'0',
|
||||
... 'form-MAX_NUM_FORMS': u'',
|
||||
... }
|
||||
>>> formset = ArticleFormSet(data)
|
||||
>>> formset.is_valid()
|
||||
True
|
||||
|
||||
|
@ -113,7 +118,7 @@ provide an invalid article::
|
|||
... 'form-INITIAL_FORMS': u'0',
|
||||
... 'form-MAX_NUM_FORMS': u'',
|
||||
... 'form-0-title': u'Test',
|
||||
... 'form-0-pub_date': u'16 June 1904',
|
||||
... 'form-0-pub_date': u'1904-06-16',
|
||||
... 'form-1-title': u'Test',
|
||||
... 'form-1-pub_date': u'', # <-- this date is missing but required
|
||||
... }
|
||||
|
@ -208,9 +213,9 @@ is where you define your own validation that works at the formset level::
|
|||
... 'form-INITIAL_FORMS': u'0',
|
||||
... 'form-MAX_NUM_FORMS': u'',
|
||||
... 'form-0-title': u'Test',
|
||||
... 'form-0-pub_date': u'16 June 1904',
|
||||
... 'form-0-pub_date': u'1904-06-16',
|
||||
... 'form-1-title': u'Test',
|
||||
... 'form-1-pub_date': u'23 June 1912',
|
||||
... 'form-1-pub_date': u'1912-06-23',
|
||||
... }
|
||||
>>> formset = ArticleFormSet(data)
|
||||
>>> formset.is_valid()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue