Ticket #11163: 11163.patch.2

File 11163.patch.2, 9.0 KB (added by akaihola, 6 years ago)

Fixed many-to-many fields and tests, too (still against r11173).

Line 
1diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
2index 8297eca..8e09064 100644
3--- a/django/contrib/admin/options.py
4+++ b/django/contrib/admin/options.py
5@@ -139,7 +139,7 @@ class BaseModelAdmin(object):
6         Get a form Field for a ForeignKey.
7         """
8         if db_field.name in self.raw_id_fields:
9-            kwargs['widget'] = widgets.ForeignKeyRawIdWidget(db_field.rel)
10+            kwargs['widget'] = widgets.ForeignKeyRawIdWidget(db_field.rel, self.admin_site)
11         elif db_field.name in self.radio_fields:
12             kwargs['widget'] = widgets.AdminRadioSelect(attrs={
13                 'class': get_ul_class(self.radio_fields[db_field.name]),
14@@ -157,7 +157,7 @@ class BaseModelAdmin(object):
15             return None
16 
17         if db_field.name in self.raw_id_fields:
18-            kwargs['widget'] = widgets.ManyToManyRawIdWidget(db_field.rel)
19+            kwargs['widget'] = widgets.ManyToManyRawIdWidget(db_field.rel, self.admin_site)
20             kwargs['help_text'] = ''
21         elif db_field.name in (list(self.filter_vertical) + list(self.filter_horizontal)):
22             kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical))
23diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
24index 7ae5e64..3315fe8 100644
25--- a/django/contrib/admin/widgets.py
26+++ b/django/contrib/admin/widgets.py
27@@ -101,14 +101,23 @@ class ForeignKeyRawIdWidget(forms.TextInput):
28     A Widget for displaying ForeignKeys in the "raw_id" interface rather than
29     in a <select> box.
30     """
31-    def __init__(self, rel, attrs=None):
32+    def __init__(self, rel, admin_site, attrs=None):
33         self.rel = rel
34+        self.admin_site = admin_site
35         super(ForeignKeyRawIdWidget, self).__init__(attrs)
36 
37     def render(self, name, value, attrs=None):
38         if attrs is None:
39             attrs = {}
40-        related_url = '../../../%s/%s/' % (self.rel.to._meta.app_label, self.rel.to._meta.object_name.lower())
41+        # TODO: DRY violation, copied code from
42+        # RelatedFieldWidgetWrapper.render() below to fix #11163
43+        rel_to = self.rel.to
44+        info = (rel_to._meta.app_label, rel_to._meta.object_name.lower())
45+        try:
46+            related_info = (self.admin_site.name,) + info
47+            related_url = reverse('%sadmin_%s_%s_changelist' % related_info)
48+        except NoReverseMatch:
49+            related_url = '../../../%s/%s/' % info
50         params = self.url_parameters()
51         if params:
52             url = '?' + '&amp;'.join(['%s=%s' % (k, v) for k, v in params.items()])
53@@ -155,8 +164,8 @@ class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
54     A Widget for displaying ManyToMany ids in the "raw_id" interface rather than
55     in a <select multiple> box.
56     """
57-    def __init__(self, rel, attrs=None):
58-        super(ManyToManyRawIdWidget, self).__init__(rel, attrs)
59+    def __init__(self, rel, admin_site, attrs=None):
60+        super(ManyToManyRawIdWidget, self).__init__(rel, admin_site, attrs)
61 
62     def render(self, name, value, attrs=None):
63         attrs['class'] = 'vManyToManyRawIdAdminField'
64diff --git a/tests/regressiontests/admin_widgets/models.py b/tests/regressiontests/admin_widgets/models.py
65index 0c81ed3..8077bea 100644
66--- a/tests/regressiontests/admin_widgets/models.py
67+++ b/tests/regressiontests/admin_widgets/models.py
68@@ -77,6 +77,7 @@ __test__ = {'WIDGETS_TESTS': """
69 >>> from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime
70 >>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget, ManyToManyRawIdWidget
71 >>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
72+>>> from widgetadmin import site
73 
74 Calling conditional_escape on the output of widget.render will simulate what
75 happens in the template. This is easier than setting up a template and context
76@@ -85,6 +86,10 @@ for each test.
77 Make sure that the Admin widgets render properly, that is, without their extra
78 HTML escaped.
79 
80+An editable ForeignKeyRawIdWidget should always use an absolute path for the
81+pop-up link.  A relative path used to fail for changelists since the URL depth
82+is different from a change page (see #11163).
83+
84 >>> w = FilteredSelectMultiple('test', False)
85 >>> print conditional_escape(w.render('test', 'test'))
86 <select multiple="multiple" name="test">
87@@ -105,18 +110,18 @@ Currently: <a target="_blank" href="%(STORAGE_URL)salbums/hybrid_theory.jpg">alb
88 <input type="file" name="test" />
89 
90 >>> rel = Album._meta.get_field('band').rel
91->>> w = ForeignKeyRawIdWidget(rel)
92+>>> w = ForeignKeyRawIdWidget(rel, site)
93 >>> print conditional_escape(w.render('test', band.pk, attrs={}))
94-<input type="text" name="test" value="1" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Linkin Park</strong>
95+<input type="text" name="test" value="1" class="vForeignKeyRawIdAdminField" /><a href="/widget_admin/admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Linkin Park</strong>
96 
97 >>> m1 = Member.objects.create(pk=1, name='Chester')
98 >>> m2 = Member.objects.create(pk=2, name='Mike')
99 >>> band.members.add(m1, m2)
100 
101 >>> rel = Band._meta.get_field('members').rel
102->>> w = ManyToManyRawIdWidget(rel)
103+>>> w = ManyToManyRawIdWidget(rel, site)
104 >>> print conditional_escape(w.render('test', [m1.pk, m2.pk], attrs={}))
105-<input type="text" name="test" value="1,2" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>
106+<input type="text" name="test" value="1,2" class="vManyToManyRawIdAdminField" /><a href="/widget_admin/admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>
107 >>> w._has_changed(None, None)
108 False
109 >>> w._has_changed([], None)
110@@ -136,15 +141,15 @@ True
111 >>> pear = Inventory.objects.create(barcode=22, name='Pear')
112 >>> core = Inventory.objects.create(barcode=87, name='Core', parent=apple)
113 >>> rel = Inventory._meta.get_field('parent').rel
114->>> w = ForeignKeyRawIdWidget(rel)
115+>>> w = ForeignKeyRawIdWidget(rel, site)
116 >>> print w.render('test', core.parent_id, attrs={})
117-<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>
118+<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="/widget_admin/admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>
119 
120 # see #9258
121 >>> hidden = Inventory.objects.create(barcode=93, name='Hidden', hidden=True)
122 >>> child_of_hidden = Inventory.objects.create(barcode=94, name='Child of hidden', parent=hidden)
123 >>> print w.render('test', child_of_hidden.parent_id, attrs={})
124-<input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Hidden</strong>
125+<input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" /><a href="/widget_admin/admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Hidden</strong>
126 """ % {
127     'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
128     'STORAGE_URL': default_storage.url(''),
129diff --git a/tests/regressiontests/admin_widgets/widgetadmin.py b/tests/regressiontests/admin_widgets/widgetadmin.py
130index bd68954..a363a3b 100644
131--- a/tests/regressiontests/admin_widgets/widgetadmin.py
132+++ b/tests/regressiontests/admin_widgets/widgetadmin.py
133@@ -24,3 +24,6 @@ site = WidgetAdmin()
134 site.register(models.User)
135 site.register(models.Car, CarAdmin)
136 site.register(models.CarTire, CarTireAdmin)
137+site.register(models.Inventory)
138+site.register(models.Band)
139+site.register(models.Member)
Back to Top