Ticket #13794: fix_inline_model_with_to_field.diff

File fix_inline_model_with_to_field.diff, 1.5 KB (added by gautier, 9 years ago)

Fix + regression test

  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 8accd61..a229dd6 100644
    a b class BaseInlineFormSet(BaseModelFormSet): 
    716716
    717717            # Remove the foreign key from the form's data
    718718            form.data[form.add_prefix(self.fk.name)] = None
    719 
    720         # Set the fk value here so that the form can do it's validation.
    721         setattr(form.instance, self.fk.get_attname(), self.instance.pk)
    722719        return form
    723720
    724721    #@classmethod
  • 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 61bc514..73228a5 100644
    a b class InlineFormsetTests(TestCase): 
    156156        # you can create a formset with an instance of None
    157157        form = Form(instance=None)
    158158        formset = FormSet(instance=None)
     159
     160    def test_inline_model_with_to_field(self):
     161        "An inline model with a to_field of a formset with instance have working relations. Regression for #13794"
     162        FormSet = inlineformset_factory(User, UserSite)
     163
     164        user = User.objects.create(username="guido", serial=1337)
     165        UserSite.objects.create(user=user, data=10)
     166        formset = FormSet(instance=user)
     167
     168        original_site1 = formset.get_queryset()[0]
     169
     170        # Testing the inline model's relation
     171        self.assertEqual(original_site1.user.username, "guido")
Back to Top