Code

Ticket #11821: multiple-fk-delete-view.txt

File multiple-fk-delete-view.txt, 4.1 KB (added by shields@…, 5 years ago)
Line 
1Index: django/contrib/admin/util.py
2===================================================================
3--- django/contrib/admin/util.py        (revision 11479)
4+++ django/contrib/admin/util.py        (working copy)
5@@ -85,12 +85,8 @@
6     nh = _nest_help # Bind to local variable for performance
7     if current_depth > 16:
8         return # Avoid recursing too deep.
9-    opts_seen = []
10     for related in opts.get_all_related_objects():
11         has_admin = related.model in admin_site._registry
12-        if related.opts in opts_seen:
13-            continue
14-        opts_seen.append(related.opts)
15         rel_opts_name = related.get_accessor_name()
16         if isinstance(related.field.rel, models.OneToOneRel):
17             try:
18@@ -148,9 +144,6 @@
19                     perms_needed.add(related.opts.verbose_name)
20     for related in opts.get_all_related_many_to_many_objects():
21         has_admin = related.model in admin_site._registry
22-        if related.opts in opts_seen:
23-            continue
24-        opts_seen.append(related.opts)
25         rel_opts_name = related.get_accessor_name()
26         has_related_objs = False
27 
28Index: tests/regressiontests/admin_views/tests.py
29===================================================================
30--- tests/regressiontests/admin_views/tests.py  (revision 11479)
31+++ tests/regressiontests/admin_views/tests.py  (working copy)
32@@ -18,7 +18,7 @@
33     ExternalSubscriber, FooAccount, Gallery, ModelWithStringPrimaryKey, \
34     Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast, \
35     Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit, \
36-    Category
37+    Category, Color, MulticoloredThing
38 
39 try:
40     set
41@@ -323,6 +323,10 @@
42 
43         delete_user.user_permissions.add(get_perm(Section,
44             Section._meta.get_delete_permission()))
45+        delete_user.user_permissions.add(get_perm(Color,
46+            Color._meta.get_delete_permission()))
47+        delete_user.user_permissions.add(get_perm(MulticoloredThing,
48+            MulticoloredThing._meta.get_delete_permission()))
49 
50         # login POST dicts
51         self.super_login = {
52@@ -584,6 +588,15 @@
53         self.failUnlessEqual(logged.object_id, u'1')
54         self.client.get('/test_admin/admin/logout/')
55 
56+    def testDeleteViewWithMultipleForeignKeysToSameTable(self):
57+        """Regression for #11821."""
58+        red = Color.objects.create(value='Red')
59+        thing1 = MulticoloredThing.objects.create(title='Thing 1', background_color=red)
60+        self.client.get('/test_admin/admin/')
61+        self.client.post('/test_admin/admin/', self.deleteuser_login)
62+        response = self.client.get('/test_admin/admin/admin_views/color/%d/delete/' % red.id)
63+        self.assertContains(response, 'admin_views/multicoloredthing/%d/' % thing1.id)
64+
65 class AdminViewStringPrimaryKeyTest(TestCase):
66     fixtures = ['admin-views-users.xml', 'string-primary-key.xml']
67 
68@@ -1605,4 +1618,3 @@
69         "Check the never-cache status of the Javascript i18n view"
70         response = self.client.get('/test_admin/jsi18n/')
71         self.failUnlessEqual(get_max_age(response), None)
72-
73Index: tests/regressiontests/admin_views/models.py
74===================================================================
75--- tests/regressiontests/admin_views/models.py (revision 11479)
76+++ tests/regressiontests/admin_views/models.py (working copy)
77@@ -138,6 +138,13 @@
78 class ThingAdmin(admin.ModelAdmin):
79     list_filter = ('color',)
80 
81+class MulticoloredThing(models.Model):
82+    title = models.CharField(max_length=20)
83+    foreground_color = models.ForeignKey(Color, related_name='thing_foreground', blank=True, null=True)
84+    background_color = models.ForeignKey(Color, related_name='thing_background', blank=True, null=True)
85+    def __unicode__(self):
86+        return self.title
87+
88 class Fabric(models.Model):
89     NG_CHOICES = (
90         ('Textured', (
91@@ -426,6 +433,7 @@
92 admin.site.register(ModelWithStringPrimaryKey)
93 admin.site.register(Color)
94 admin.site.register(Thing, ThingAdmin)
95+admin.site.register(MulticoloredThing)
96 admin.site.register(Person, PersonAdmin)
97 admin.site.register(Persona, PersonaAdmin)
98 admin.site.register(Subscriber, SubscriberAdmin)