diff --git a/django/forms/models.py b/django/forms/models.py
index ecfdc92..bd15d34 100644
a
|
b
|
class BaseInlineFormSet(BaseModelFormSet):
|
697 | 697 | # Remove the foreign key from the form's data |
698 | 698 | form.data[form.add_prefix(self.fk.name)] = None |
699 | 699 | |
700 | | # Set the fk value here so that the form can do it's validation. |
701 | | setattr(form.instance, self.fk.get_attname(), self.instance.pk) |
702 | 700 | return form |
703 | 701 | |
704 | 702 | @classmethod |
diff --git a/tests/regressiontests/model_formsets_regress/tests.py b/tests/regressiontests/model_formsets_regress/tests.py
index e6c2633..5528184 100644
a
|
b
|
class InlineFormsetTests(TestCase):
|
161 | 161 | form = Form(instance=None) |
162 | 162 | formset = FormSet(instance=None) |
163 | 163 | |
| 164 | def test_inline_model_with_to_field(self): |
| 165 | "An inline model with a to_field of a formset with instance have working relations. Regression for #13794" |
| 166 | FormSet = inlineformset_factory(User, UserSite) |
| 167 | |
| 168 | user = User.objects.create(username="guido", serial=1337) |
| 169 | UserSite.objects.create(user=user, data=10) |
| 170 | formset = FormSet(instance=user) |
| 171 | |
| 172 | original_site1 = formset.get_queryset()[0] |
| 173 | |
| 174 | # Testing the inline model's relation |
| 175 | self.assertEqual(original_site1.user.username, "guido") |
| 176 | |
164 | 177 | def test_empty_fields_on_modelformset(self): |
165 | 178 | "No fields passed to modelformset_factory should result in no fields on returned forms except for the id. See #14119." |
166 | 179 | UserFormSet = modelformset_factory(User, fields=()) |