diff --git a/django/forms/models.py b/django/forms/models.py index cc43612bf5..eca0259878 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -709,7 +709,7 @@ class BaseInlineFormSet(BaseModelFormSet): save_as_new=False, prefix=None): from django.db.models.fields.related import RelatedObject if instance is None: - self.instance = self.model() + self.instance = self.fk.rel.to() else: self.instance = instance self.save_as_new = save_as_new diff --git a/tests/regressiontests/model_formsets_regress/tests.py b/tests/regressiontests/model_formsets_regress/tests.py index f5e9355e42..4dba9fc19f 100644 --- a/tests/regressiontests/model_formsets_regress/tests.py +++ b/tests/regressiontests/model_formsets_regress/tests.py @@ -140,3 +140,13 @@ class InlineFormsetTests(TestCase): self.assertEqual(manager[1]['name'], 'Terry Gilliam') else: self.fail('Errors found on formset:%s' % form_set.errors) + + def test_formset_with_none_instance(self): + "A formset with instance=None can be created. Regression for #11872" + Form = modelform_factory(User) + FormSet = inlineformset_factory(User, UserSite) + + # Instantiate the Form and FormSet to prove + # you can create a formset with an instance of None + form = Form(instance=None) + formset = FormSet(instance=None)