Ticket #11418: 11418.patch

File 11418.patch, 5.8 KB (added by Mark Lavin, 14 years ago)

New patch vs [13738] with doc updates

  • django/forms/formsets.py

    diff --git a/django/forms/formsets.py b/django/forms/formsets.py
    a b  
    3737        self.is_bound = data is not None or files is not None
    3838        self.prefix = prefix or self.get_default_prefix()
    3939        self.auto_id = auto_id
    40         self.data = data
    41         self.files = files
     40        self.data = data or {}
     41        self.files = files or {}
    4242        self.initial = initial
    4343        self.error_class = error_class
    4444        self._errors = None
     
    5151
    5252    def _management_form(self):
    5353        """Returns the ManagementForm instance for this FormSet."""
    54         if self.data or self.files:
     54        if self.is_bound:
    5555            form = ManagementForm(self.data, auto_id=self.auto_id, prefix=self.prefix)
    5656            if not form.is_valid():
    5757                raise ValidationError('ManagementForm data is missing or has been tampered with')
     
    6666
    6767    def total_form_count(self):
    6868        """Returns the total number of forms in this FormSet."""
    69         if self.data or self.files:
     69        if self.is_bound:
    7070            return self.management_form.cleaned_data[TOTAL_FORM_COUNT]
    7171        else:
    7272            initial_forms = self.initial_form_count()
     
    8181
    8282    def initial_form_count(self):
    8383        """Returns the number of forms that are required in this FormSet."""
    84         if self.data or self.files:
     84        if self.is_bound:
    8585            return self.management_form.cleaned_data[INITIAL_FORM_COUNT]
    8686        else:
    8787            # Use the length of the inital data if it's there, 0 otherwise.
     
    101101        Instantiates and returns the i-th form instance in a formset.
    102102        """
    103103        defaults = {'auto_id': self.auto_id, 'prefix': self.add_prefix(i)}
    104         if self.data or self.files:
     104        if self.is_bound:
    105105            defaults['data'] = self.data
    106106            defaults['files'] = self.files
    107107        if self.initial:
     
    133133            'prefix': self.add_prefix('__prefix__'),
    134134            'empty_permitted': True,
    135135        }
    136         if self.data or self.files:
     136        if self.is_bound:
    137137            defaults['data'] = self.data
    138138            defaults['files'] = self.files
    139139        defaults.update(kwargs)
  • docs/topics/forms/formsets.txt

    diff --git a/docs/topics/forms/formsets.txt b/docs/topics/forms/formsets.txt
    a b  
    100100all forms in the formset::
    101101
    102102    >>> ArticleFormSet = formset_factory(ArticleForm)
    103     >>> formset = ArticleFormSet({})
     103    >>> data = {
     104    ...     'form-TOTAL_FORMS': u'1',
     105    ...     'form-INITIAL_FORMS': u'0',
     106    ...     'form-MAX_NUM_FORMS': u'',
     107    ... }
     108    >>> formset = ArticleFormSet(data)
    104109    >>> formset.is_valid()
    105110    True
    106111
     
    113118    ...     'form-INITIAL_FORMS': u'0',
    114119    ...     'form-MAX_NUM_FORMS': u'',
    115120    ...     'form-0-title': u'Test',
    116     ...     'form-0-pub_date': u'16 June 1904',
     121    ...     'form-0-pub_date': u'1904-06-16',
    117122    ...     'form-1-title': u'Test',
    118123    ...     'form-1-pub_date': u'', # <-- this date is missing but required
    119124    ... }
     
    208213    ...     'form-INITIAL_FORMS': u'0',
    209214    ...     'form-MAX_NUM_FORMS': u'',
    210215    ...     'form-0-title': u'Test',
    211     ...     'form-0-pub_date': u'16 June 1904',
     216    ...     'form-0-pub_date': u'1904-06-16',
    212217    ...     'form-1-title': u'Test',
    213     ...     'form-1-pub_date': u'23 June 1912',
     218    ...     'form-1-pub_date': u'1912-06-23',
    214219    ... }
    215220    >>> formset = ArticleFormSet(data)
    216221    >>> formset.is_valid()
  • tests/regressiontests/forms/formsets.py

    diff --git a/tests/regressiontests/forms/formsets.py b/tests/regressiontests/forms/formsets.py
    a b  
    55FormSet allows us to use multiple instance of the same form on 1 page. For now,
    66the best way to create a FormSet is by using the formset_factory function.
    77
    8 >>> from django.forms import Form, CharField, IntegerField, ValidationError
     8>>> from django.forms import Form, CharField, IntegerField, ValidationError, DateField
    99>>> from django.forms.formsets import formset_factory, BaseFormSet
    1010
    1111>>> class Choice(Form):
     
    697697>>> formset.management_form.prefix
    698698'form'
    699699
    700 >>> formset = FavoriteDrinksFormSet(data={})
     700>>> data = {
     701...     'form-TOTAL_FORMS': '2', # the number of forms rendered
     702...     'form-INITIAL_FORMS': '0', # the number of forms with initial data
     703...     'form-MAX_NUM_FORMS': '0', # max number of forms
     704... }
     705>>> formset = FavoriteDrinksFormSet(data=data)
    701706>>> formset.management_form.prefix
    702707'form'
    703708
     
    721726>>> print formset.non_form_errors()
    722727<ul class="errorlist"><li>You may only specify a drink once.</li></ul>
    723728
     729# Regression test for #11418 #################################################
     730
     731>>> class ArticleForm(Form):
     732...     title = CharField()
     733...     pub_date = DateField()
     734
     735>>> ArticleFormSet = formset_factory(ArticleForm)
     736>>> formset = ArticleFormSet({})
     737Traceback (most recent call last):
     738...
     739ValidationError: [u'ManagementForm data is missing or has been tampered with']
     740>>> data = {
     741...     'form-TOTAL_FORMS': u'1',
     742...     'form-INITIAL_FORMS': u'0',
     743... }
     744>>> formset = ArticleFormSet(data)
     745>>> formset.initial_form_count()
     7460
     747>>> formset.total_form_count()
     7481
     749>>> formset.is_bound
     750True
     751>>> formset.forms[0].is_bound
     752True
     753>>> formset.is_valid()
     754True
     755>>> formset.forms[0].is_valid()
     756True
     757>>> formset.cleaned_data
     758[{}]
     759>>> data = {
     760...     'form-TOTAL_FORMS': u'2',
     761...     'form-INITIAL_FORMS': u'0',
     762...     'form-0-title': u'Test',
     763...     'form-0-pub_date': u'1904-06-16',
     764...     'form-1-title': u'Test',
     765...     'form-1-pub_date': u'', # <-- this date is missing but required
     766... }
     767>>> formset = ArticleFormSet(data)
     768>>> formset.is_valid()
     769False
     770>>> formset.errors
     771[{}, {'pub_date': [u'This field is required.']}]
     772
    724773"""
Back to Top