Ticket #13794: to_field_formsets.4.diff
File to_field_formsets.4.diff, 3.6 KB (added by , 10 years ago) |
---|
-
django/forms/models.py
diff --git a/django/forms/models.py b/django/forms/models.py index 9e6cab8..84da2f7 100644
a b class BaseInlineFormSet(BaseModelFormSet): 876 876 form.data[form.add_prefix(self.fk.name)] = None 877 877 878 878 # Set the fk value here so that the form can do its validation. 879 setattr(form.instance, self.fk.get_attname(), self.instance.pk) 879 fk_value = self.instance.pk 880 if self.fk.rel.field_name != self.fk.rel.to._meta.pk.name: 881 fk_value = getattr(self.instance, self.fk.rel.field_name) 882 fk_value = getattr(fk_value, 'pk', fk_value) 883 setattr(form.instance, self.fk.get_attname(), fk_value) 880 884 return form 881 885 882 886 @classmethod -
tests/model_formsets_regress/models.py
diff --git a/tests/model_formsets_regress/models.py b/tests/model_formsets_regress/models.py index 77ee5a2..c9ded9e 100644
a b class UserSite(models.Model): 12 12 data = models.IntegerField() 13 13 14 14 15 class UserProfile(models.Model): 16 user = models.ForeignKey(User, unique=True, to_field="username") 17 about = models.TextField() 18 19 20 class ProfileNetwork(models.Model): 21 profile = models.ForeignKey(UserProfile, to_field="user") 22 network = models.IntegerField() 23 identifier = models.IntegerField() 24 25 15 26 class Place(models.Model): 16 27 name = models.CharField(max_length=50) 17 28 -
tests/model_formsets_regress/tests.py
diff --git a/tests/model_formsets_regress/tests.py b/tests/model_formsets_regress/tests.py index 7d400d2..c5dd8f0 100644
a b from django.forms.models import modelform_factory, inlineformset_factory, modelf 7 7 from django.test import TestCase 8 8 from django.utils import six 9 9 10 from .models import User, UserSite, Restaurant, Manager, Network, Host10 from .models import User, UserSite, UserProfile, ProfileNetwork, Restaurant, Manager, Network, Host 11 11 12 12 13 13 class InlineFormsetTests(TestCase): … … class InlineFormsetTests(TestCase): 154 154 else: 155 155 self.fail('Errors found on formset:%s' % form_set.errors) 156 156 157 def test_inline_model_with_to_field(self): 158 "An inline model with a to_field of a formset with instance have working relations. Regression for #13794" 159 FormSet = inlineformset_factory(User, UserSite, exclude=('is_superuser',)) 160 161 user = User.objects.create(username="guido", serial=1337) 162 UserSite.objects.create(user=user, data=10) 163 formset = FormSet(instance=user) 164 165 formset[0] 166 # Testing the inline model's relation 167 self.assertEqual(formset[0].instance.user_id, "guido") 168 169 def test_inline_model_with_to_field_to_rel(self): 170 "An inline model with a to_field to related field of a formset with instance have working relations. Regression for #13794" 171 FormSet = inlineformset_factory(UserProfile, ProfileNetwork, exclude=[]) 172 173 user = User.objects.create(username="guido", serial=1337, pk=1) 174 self.assertEqual(user.pk, 1) 175 profile = UserProfile.objects.create(user=user, about="about", pk=2) 176 self.assertEqual(profile.pk, 2) 177 ProfileNetwork.objects.create(profile=profile, network=10, identifier=10) 178 formset = FormSet(instance=profile) 179 180 formset[0] 181 # Testing the inline model's relation 182 self.assertEqual(formset[0].instance.profile_id, 1) 183 157 184 def test_formset_with_none_instance(self): 158 185 "A formset with instance=None can be created. Regression for #11872" 159 186 Form = modelform_factory(User, fields="__all__")