diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
index 291bee0..e988195 100644
a
|
b
|
from django.utils.translation import ugettext as _
|
12 | 12 | from django.utils.safestring import mark_safe |
13 | 13 | from django.utils.encoding import force_unicode |
14 | 14 | from django.conf import settings |
| 15 | from django.core.urlresolvers import reverse, NoReverseMatch |
15 | 16 | |
16 | 17 | class FilteredSelectMultiple(forms.SelectMultiple): |
17 | 18 | """ |
… |
… |
class RelatedFieldWidgetWrapper(forms.Widget):
|
219 | 220 | |
220 | 221 | def render(self, name, value, *args, **kwargs): |
221 | 222 | rel_to = self.rel.to |
222 | | related_url = '../../../%s/%s/' % (rel_to._meta.app_label, rel_to._meta.object_name.lower()) |
| 223 | info = (rel_to._meta.app_label, rel_to._meta.object_name.lower()) |
| 224 | try: |
| 225 | info = (self.admin_site.name,) + info |
| 226 | related_url = reverse('%sadmin_%s_%s_add' % info) |
| 227 | except NoReverseMatch: |
| 228 | related_url = '../../../%s/%s/add/' % info |
223 | 229 | self.widget.choices = self.choices |
224 | 230 | output = [self.widget.render(name, value, *args, **kwargs)] |
225 | 231 | if rel_to in self.admin_site._registry: # If the related object has an admin interface: |
226 | 232 | # TODO: "id_" is hard-coded here. This should instead use the correct |
227 | 233 | # API to determine the ID dynamically. |
228 | | output.append(u'<a href="%sadd/" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \ |
| 234 | output.append(u'<a href="%s" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \ |
229 | 235 | (related_url, name)) |
230 | 236 | output.append(u'<img src="%simg/admin/icon_addlink.gif" width="10" height="10" alt="%s"/></a>' % (settings.ADMIN_MEDIA_PREFIX, _('Add Another'))) |
231 | 237 | return mark_safe(u''.join(output)) |
diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py
index 4dc2930..c2b0403 100644
a
|
b
|
class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase):
|
110 | 110 | response = self.client.get("/widget_admin/admin_widgets/cartire/add/") |
111 | 111 | self.assert_("BMW M3" not in response.content) |
112 | 112 | self.assert_("Volkswagon Passat" in response.content) |
| 113 | |
| 114 | class AdminForeignKeyWidgetChangeList(DjangoTestCase): |
| 115 | fixtures = ["admin-widgets-users.xml"] |
| 116 | |
| 117 | def setUp(self): |
| 118 | self.client.login(username="super", password="secret") |
| 119 | |
| 120 | def tearDown(self): |
| 121 | self.client.logout() |
| 122 | |
| 123 | def test_changelist_foreignkey(self): |
| 124 | response = self.client.get('/widget_admin/admin_widgets/car/') |
| 125 | self.failUnless('/widget_admin/auth/user/add/' in response.content) |
diff --git a/tests/regressiontests/admin_widgets/widgetadmin.py b/tests/regressiontests/admin_widgets/widgetadmin.py
index bc28dac..bd68954 100644
a
|
b
|
import models
|
8 | 8 | class WidgetAdmin(admin.AdminSite): |
9 | 9 | pass |
10 | 10 | |
| 11 | class CarAdmin(admin.ModelAdmin): |
| 12 | list_display = ['make', 'model', 'owner'] |
| 13 | list_editable = ['owner'] |
11 | 14 | |
12 | 15 | class CarTireAdmin(admin.ModelAdmin): |
13 | 16 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
… |
… |
class CarTireAdmin(admin.ModelAdmin):
|
18 | 21 | |
19 | 22 | site = WidgetAdmin() |
20 | 23 | |
21 | | site.register(models.Car) |
| 24 | site.register(models.User) |
| 25 | site.register(models.Car, CarAdmin) |
22 | 26 | site.register(models.CarTire, CarTireAdmin) |