diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index 291bee0b05..7ae5e647db 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -12,6 +12,7 @@ from django.utils.translation import ugettext as _ from django.utils.safestring import mark_safe from django.utils.encoding import force_unicode from django.conf import settings +from django.core.urlresolvers import reverse, NoReverseMatch class FilteredSelectMultiple(forms.SelectMultiple): """ @@ -219,13 +220,18 @@ class RelatedFieldWidgetWrapper(forms.Widget): def render(self, name, value, *args, **kwargs): rel_to = self.rel.to - related_url = '../../../%s/%s/' % (rel_to._meta.app_label, rel_to._meta.object_name.lower()) + info = (rel_to._meta.app_label, rel_to._meta.object_name.lower()) + try: + related_info = (self.admin_site.name,) + info + related_url = reverse('%sadmin_%s_%s_add' % related_info) + except NoReverseMatch: + related_url = '../../../%s/%s/add/' % info self.widget.choices = self.choices output = [self.widget.render(name, value, *args, **kwargs)] if rel_to in self.admin_site._registry: # If the related object has an admin interface: # TODO: "id_" is hard-coded here. This should instead use the correct # API to determine the ID dynamically. - output.append(u' ' % \ + output.append(u' ' % \ (related_url, name)) output.append(u'%s' % (settings.ADMIN_MEDIA_PREFIX, _('Add Another'))) return mark_safe(u''.join(output)) diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py index 4dc2930e3b..c2b040368d 100644 --- a/tests/regressiontests/admin_widgets/tests.py +++ b/tests/regressiontests/admin_widgets/tests.py @@ -110,3 +110,16 @@ class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase): response = self.client.get("/widget_admin/admin_widgets/cartire/add/") self.assert_("BMW M3" not in response.content) self.assert_("Volkswagon Passat" in response.content) + +class AdminForeignKeyWidgetChangeList(DjangoTestCase): + fixtures = ["admin-widgets-users.xml"] + + def setUp(self): + self.client.login(username="super", password="secret") + + def tearDown(self): + self.client.logout() + + def test_changelist_foreignkey(self): + response = self.client.get('/widget_admin/admin_widgets/car/') + 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 bc28dac86a..bd68954a70 100644 --- a/tests/regressiontests/admin_widgets/widgetadmin.py +++ b/tests/regressiontests/admin_widgets/widgetadmin.py @@ -8,6 +8,9 @@ import models class WidgetAdmin(admin.AdminSite): pass +class CarAdmin(admin.ModelAdmin): + list_display = ['make', 'model', 'owner'] + list_editable = ['owner'] class CarTireAdmin(admin.ModelAdmin): def formfield_for_foreignkey(self, db_field, request, **kwargs): @@ -18,5 +21,6 @@ class CarTireAdmin(admin.ModelAdmin): site = WidgetAdmin() -site.register(models.Car) +site.register(models.User) +site.register(models.Car, CarAdmin) site.register(models.CarTire, CarTireAdmin)