Ticket #13794: ticket13794.patch

File ticket13794.patch, 1.7 KB (added by gautier, 14 years ago)

Fix + regression test, updated for new trunk

  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index ecfdc92..bd15d34 100644
    a b class BaseInlineFormSet(BaseModelFormSet):  
    697697            # Remove the foreign key from the form's data
    698698            form.data[form.add_prefix(self.fk.name)] = None
    699699
    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)
    702700        return form
    703701
    704702    @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 e6c2633..5528184 100644
    a b class InlineFormsetTests(TestCase):  
    161161        form = Form(instance=None)
    162162        formset = FormSet(instance=None)
    163163
     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
    164177    def test_empty_fields_on_modelformset(self):
    165178        "No fields passed to modelformset_factory should result in no fields on returned forms except for the id. See #14119."
    166179        UserFormSet = modelformset_factory(User, fields=())
Back to Top