Ticket #13794: to_field_formsets.4.diff

File to_field_formsets.4.diff, 3.6 KB (added by anonymous, 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):  
    876876            form.data[form.add_prefix(self.fk.name)] = None
    877877
    878878        # 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)
    880884        return form
    881885
    882886    @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):  
    1212    data = models.IntegerField()
    1313
    1414
     15class UserProfile(models.Model):
     16    user = models.ForeignKey(User, unique=True, to_field="username")
     17    about = models.TextField()
     18
     19
     20class ProfileNetwork(models.Model):
     21    profile = models.ForeignKey(UserProfile, to_field="user")
     22    network = models.IntegerField()
     23    identifier = models.IntegerField()
     24
     25
    1526class Place(models.Model):
    1627    name = models.CharField(max_length=50)
    1728
  • 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  
    77from django.test import TestCase
    88from django.utils import six
    99
    10 from .models import User, UserSite, Restaurant, Manager, Network, Host
     10from .models import User, UserSite, UserProfile, ProfileNetwork, Restaurant, Manager, Network, Host
    1111
    1212
    1313class InlineFormsetTests(TestCase):
    class InlineFormsetTests(TestCase):  
    154154        else:
    155155            self.fail('Errors found on formset:%s' % form_set.errors)
    156156
     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
    157184    def test_formset_with_none_instance(self):
    158185        "A formset with instance=None can be created. Regression for #11872"
    159186        Form = modelform_factory(User, fields="__all__")
Back to Top