diff --git a/django/forms/formsets.py b/django/forms/formsets.py
index 9cdd051..dcd2f01 100644
a
|
b
|
class BaseFormSet(StrAndUnicode):
|
122 | 122 | if self.is_bound: |
123 | 123 | defaults['data'] = self.data |
124 | 124 | defaults['files'] = self.files |
125 | | if self.initial: |
| 125 | if self.initial and not 'initial' in kwargs: |
126 | 126 | try: |
127 | 127 | defaults['initial'] = self.initial[i] |
128 | 128 | except IndexError: |
diff --git a/django/forms/models.py b/django/forms/models.py
index b65f067..15d5041 100644
a
|
b
|
class BaseModelFormSet(BaseFormSet):
|
418 | 418 | def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, |
419 | 419 | queryset=None, **kwargs): |
420 | 420 | self.queryset = queryset |
| 421 | self.initial_extra = kwargs.pop('initial', None) |
421 | 422 | defaults = {'data': data, 'files': files, 'auto_id': auto_id, 'prefix': prefix} |
422 | 423 | defaults.update(kwargs) |
423 | 424 | super(BaseModelFormSet, self).__init__(**defaults) |
… |
… |
class BaseModelFormSet(BaseFormSet):
|
448 | 449 | kwargs['instance'] = self._existing_object(pk) |
449 | 450 | if i < self.initial_form_count() and not kwargs.get('instance'): |
450 | 451 | kwargs['instance'] = self.get_queryset()[i] |
| 452 | if i >= self.initial_form_count() and self.initial_extra: |
| 453 | # Set initial values for extra forms |
| 454 | try: |
| 455 | kwargs['initial'] = self.initial_extra[i-self.initial_form_count()] |
| 456 | except IndexError: |
| 457 | pass |
451 | 458 | return super(BaseModelFormSet, self)._construct_form(i, **kwargs) |
452 | 459 | |
453 | 460 | def get_queryset(self): |
… |
… |
def modelformset_factory(model, form=ModelForm, formfield_callback=None,
|
674 | 681 | class BaseInlineFormSet(BaseModelFormSet): |
675 | 682 | """A formset for child objects related to a parent.""" |
676 | 683 | def __init__(self, data=None, files=None, instance=None, |
677 | | save_as_new=False, prefix=None, queryset=None): |
| 684 | save_as_new=False, prefix=None, queryset=None, **kwargs): |
678 | 685 | from django.db.models.fields.related import RelatedObject |
679 | 686 | if instance is None: |
680 | 687 | self.instance = self.fk.rel.to() |
… |
… |
class BaseInlineFormSet(BaseModelFormSet):
|
687 | 694 | queryset = self.model._default_manager |
688 | 695 | qs = queryset.filter(**{self.fk.name: self.instance}) |
689 | 696 | super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix, |
690 | | queryset=qs) |
| 697 | queryset=qs, **kwargs) |
691 | 698 | |
692 | 699 | def initial_form_count(self): |
693 | 700 | if self.save_as_new: |
diff --git a/docs/topics/forms/formsets.txt b/docs/topics/forms/formsets.txt
index 37c3067..b524c24 100644
a
|
b
|
Formsets can also be indexed into, which returns the corresponding form. If you
|
53 | 53 | override ``__iter__``, you will need to also override ``__getitem__`` to have |
54 | 54 | matching behavior. |
55 | 55 | |
| 56 | .. _formsets-initial-data: |
| 57 | |
56 | 58 | Using initial data with a formset |
57 | 59 | --------------------------------- |
58 | 60 | |
diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt
index 832a3ac..e047b6e 100644
a
|
b
|
exclude::
|
617 | 617 | |
618 | 618 | >>> AuthorFormSet = modelformset_factory(Author, exclude=('birth_date',)) |
619 | 619 | |
| 620 | Providing initial values |
| 621 | ------------------------ |
| 622 | |
| 623 | .. versionadded:: 1.4 |
| 624 | |
| 625 | As with regular formsets, it is possible to :ref:`specify initial data |
| 626 | <formsets-initial-data>` for forms in the formset. However, with model formsets |
| 627 | the initial values only apply to extra forms, those which are not bound with an |
| 628 | existing object instance. |
| 629 | |
| 630 | |
620 | 631 | .. _saving-objects-in-the-formset: |
621 | 632 | |
622 | 633 | Saving objects in the formset |
diff --git a/tests/regressiontests/model_formsets_regress/tests.py b/tests/regressiontests/model_formsets_regress/tests.py
index 35a794a..b1ad201 100644
a
|
b
|
class InlineFormsetTests(TestCase):
|
204 | 204 | ["<Host: matrix.de.eu.dal.net>", "<Host: tranquility.hub.dal.net>"] |
205 | 205 | ) |
206 | 206 | |
| 207 | def test_initial_data(self): |
| 208 | user = User.objects.create(username="bibi", serial=1) |
| 209 | usersite = UserSite.objects.create(user=user, data=7) |
| 210 | Form = modelform_factory(User) |
| 211 | FormSet = inlineformset_factory(User, UserSite, extra=2) |
| 212 | |
| 213 | formset = FormSet(instance=user, initial=[{'data': 41}, {'data': 42}]) |
| 214 | self.assertEqual(formset.forms[0].initial['data'], 7) |
| 215 | self.assertEqual(formset.extra_forms[0].initial['data'], 41) |
| 216 | self.assertTrue(u'value="42"' in formset.extra_forms[1].as_p()) |
| 217 | |
| 218 | |
207 | 219 | class FormsetTests(TestCase): |
208 | 220 | def test_error_class(self): |
209 | 221 | ''' |
… |
… |
class FormsetTests(TestCase):
|
230 | 242 | self.assertTrue(isinstance(form.errors, ErrorDict)) |
231 | 243 | self.assertTrue(isinstance(form.non_field_errors(), ErrorList)) |
232 | 244 | |
| 245 | def test_initial_data(self): |
| 246 | user = User.objects.create(username="bibi", serial=1) |
| 247 | Formset = modelformset_factory(User, extra=2) |
| 248 | formset = Formset(initial=[{'username': u'apollo11'}, {'username': u'apollo12'}]) |
| 249 | self.assertEqual(formset.forms[0].initial['username'], "bibi") |
| 250 | self.assertEqual(formset.extra_forms[0].initial['username'], "apollo11") |
| 251 | self.assertTrue(u'value="apollo12"' in formset.extra_forms[1].as_p()) |
| 252 | |
233 | 253 | class CustomWidget(forms.CharField): |
234 | 254 | pass |
235 | 255 | |