Ticket #14574: 14574-2.diff

File 14574-2.diff, 5.7 KB (added by Claude Paroz, 13 years ago)

patch including tests

  • django/forms/formsets.py

    diff --git a/django/forms/formsets.py b/django/forms/formsets.py
    index 04057a1..a2c886c 100644
    a b class BaseFormSet(StrAndUnicode):  
    119119        if self.is_bound:
    120120            defaults['data'] = self.data
    121121            defaults['files'] = self.files
    122         if self.initial:
     122        if self.initial and not 'initial' in kwargs:
    123123            try:
    124124                defaults['initial'] = self.initial[i]
    125             except IndexError:
     125            except (IndexError, KeyError):
    126126                pass
    127127        # Allow extra forms to be empty.
    128128        if i >= self.initial_form_count():
  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 254cca3..06cd53b 100644
    a b class BaseModelFormSet(BaseFormSet):  
    417417    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
    418418                 queryset=None, **kwargs):
    419419        self.queryset = queryset
     420        self.initial_extra = kwargs.pop('initial', None)
    420421        defaults = {'data': data, 'files': files, 'auto_id': auto_id, 'prefix': prefix}
    421422        defaults.update(kwargs)
    422423        super(BaseModelFormSet, self).__init__(**defaults)
    class BaseModelFormSet(BaseFormSet):  
    447448            kwargs['instance'] = self._existing_object(pk)
    448449        if i < self.initial_form_count() and not kwargs.get('instance'):
    449450            kwargs['instance'] = self.get_queryset()[i]
     451        if i >= self.initial_form_count() and self.initial_extra:
     452            # Set initial values for extra forms
     453            try:
     454                kwargs['initial'] = self.initial_extra[i-self.initial_form_count()]
     455            except IndexError:
     456                pass
    450457        return super(BaseModelFormSet, self)._construct_form(i, **kwargs)
    451458
    452459    def get_queryset(self):
    def modelformset_factory(model, form=ModelForm, formfield_callback=None,  
    673680class BaseInlineFormSet(BaseModelFormSet):
    674681    """A formset for child objects related to a parent."""
    675682    def __init__(self, data=None, files=None, instance=None,
    676                  save_as_new=False, prefix=None, queryset=None):
     683                 save_as_new=False, prefix=None, queryset=None, **kwargs):
    677684        from django.db.models.fields.related import RelatedObject
    678685        if instance is None:
    679686            self.instance = self.fk.rel.to()
    class BaseInlineFormSet(BaseModelFormSet):  
    686693            queryset = self.model._default_manager
    687694        qs = queryset.filter(**{self.fk.name: self.instance})
    688695        super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix,
    689                                                 queryset=qs)
     696                                                queryset=qs, **kwargs)
    690697
    691698    def initial_form_count(self):
    692699        if self.save_as_new:
  • docs/topics/forms/formsets.txt

    diff --git a/docs/topics/forms/formsets.txt b/docs/topics/forms/formsets.txt
    index 7912294..f021bb6 100644
    a b Formsets can also be indexed into, which returns the corresponding form. If you  
    5353override ``__iter__``, you will need to also override ``__getitem__`` to have
    5454matching behavior.
    5555
     56.. _formsets-initial-data:
     57
    5658Using initial data with a formset
    5759---------------------------------
    5860
  • docs/topics/forms/modelforms.txt

    diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt
    index 07c7b02..8b0ce31 100644
    a b exclude::  
    613613
    614614    >>> AuthorFormSet = modelformset_factory(Author, exclude=('birth_date',))
    615615
     616Providing initial values
     617------------------------
     618
     619    .. versionadded:: 1.4
     620
     621As with regular formsets, it is possible to :ref:`specify initial data
     622<formsets-initial-data>` for forms in the formset. However, with model formsets
     623the initial values only apply to extra forms, those which are not bound with an
     624existing object instance.
     625
     626
    616627.. _saving-objects-in-the-formset:
    617628
    618629Saving objects in the formset
  • tests/regressiontests/model_formsets_regress/tests.py

    diff --git a/tests/regressiontests/model_formsets_regress/tests.py b/tests/regressiontests/model_formsets_regress/tests.py
    index e6c2633..7d5a756 100644
    a b class InlineFormsetTests(TestCase):  
    202202            ["<Host: matrix.de.eu.dal.net>", "<Host: tranquility.hub.dal.net>"]
    203203            )
    204204
     205    def test_initial_data(self):
     206        user = User.objects.create(username="bibi", serial=1)
     207        usersite = UserSite.objects.create(user=user, data=7)
     208        Form = modelform_factory(User)
     209        FormSet = inlineformset_factory(User, UserSite, extra=2)
     210
     211        formset = FormSet(instance=user, initial=[{'data': 41}, {'data': 42}])
     212        self.assertEqual(formset.forms[0].initial['data'], 7)
     213        self.assertEqual(formset.extra_forms[0].initial['data'], 41)
     214        self.assertTrue(u'value="42"' in formset.extra_forms[1].as_p())
     215
     216
    205217class FormsetTests(TestCase):
    206218    def test_error_class(self):
    207219        '''
    class FormsetTests(TestCase):  
    228240            self.assertTrue(isinstance(form.errors, ErrorDict))
    229241            self.assertTrue(isinstance(form.non_field_errors(), ErrorList))
    230242
     243    def test_initial_data(self):
     244        user = User.objects.create(username="bibi", serial=1)
     245        Formset = modelformset_factory(User, extra=2)
     246        formset = Formset(initial=[{'username': u'apollo11'}, {'username': u'apollo12'}])
     247        self.assertEqual(formset.forms[0].initial['username'], "bibi")
     248        self.assertEqual(formset.extra_forms[0].initial['username'], "apollo11")
     249        self.assertTrue(u'value="apollo12"' in formset.extra_forms[1].as_p())
     250
    231251class CustomWidget(forms.CharField):
    232252    pass
    233253
Back to Top