|
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, Host |
| 10 | +from .models import ( |
| 11 | + User, UserSite, UserProfile, ProfileNetwork, Restaurant, Manager, Network, |
| 12 | + Host, |
| 13 | +) |
11 | 14 |
|
12 | 15 |
|
13 | 16 | class InlineFormsetTests(TestCase):
|
@@ -154,6 +157,37 @@ def test_formset_over_inherited_model(self):
|
154 | 157 | else:
|
155 | 158 | self.fail('Errors found on formset:%s' % form_set.errors)
|
156 | 159 |
|
| 160 | + def test_inline_model_with_to_field(self): |
| 161 | + """ |
| 162 | + #13794 --- An inline model with a to_field of a formset with instance |
| 163 | + has working relations. |
| 164 | + """ |
| 165 | + FormSet = inlineformset_factory(User, UserSite, exclude=('is_superuser',)) |
| 166 | + |
| 167 | + user = User.objects.create(username="guido", serial=1337) |
| 168 | + UserSite.objects.create(user=user, data=10) |
| 169 | + formset = FormSet(instance=user) |
| 170 | + |
| 171 | + # Testing the inline model's relation |
| 172 | + self.assertEqual(formset[0].instance.user_id, "guido") |
| 173 | + |
| 174 | + def test_inline_model_with_to_field_to_rel(self): |
| 175 | + """ |
| 176 | + #13794 --- An inline model with a to_field to a related field of a |
| 177 | + formset with instance has working relations. |
| 178 | + """ |
| 179 | + FormSet = inlineformset_factory(UserProfile, ProfileNetwork, exclude=[]) |
| 180 | + |
| 181 | + user = User.objects.create(username="guido", serial=1337, pk=1) |
| 182 | + self.assertEqual(user.pk, 1) |
| 183 | + profile = UserProfile.objects.create(user=user, about="about", pk=2) |
| 184 | + self.assertEqual(profile.pk, 2) |
| 185 | + ProfileNetwork.objects.create(profile=profile, network=10, identifier=10) |
| 186 | + formset = FormSet(instance=profile) |
| 187 | + |
| 188 | + # Testing the inline model's relation |
| 189 | + self.assertEqual(formset[0].instance.profile_id, 1) |
| 190 | + |
157 | 191 | def test_formset_with_none_instance(self):
|
158 | 192 | "A formset with instance=None can be created. Regression for #11872"
|
159 | 193 | Form = modelform_factory(User)
|
|
0 commit comments