Ticket #10537: changelist_foreignkey.diff

File changelist_foreignkey.diff, 3.6 KB (added by Alex Gaynor, 12 years ago)
  • django/contrib/admin/widgets.py

    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 _ 
    1212from django.utils.safestring import mark_safe
    1313from django.utils.encoding import force_unicode
    1414from django.conf import settings
     15from django.core.urlresolvers import reverse, NoReverseMatch
    1516
    1617class FilteredSelectMultiple(forms.SelectMultiple):
    1718    """
    class RelatedFieldWidgetWrapper(forms.Widget): 
    219220
    220221    def render(self, name, value, *args, **kwargs):
    221222        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
    223229        self.widget.choices = self.choices
    224230        output = [self.widget.render(name, value, *args, **kwargs)]
    225231        if rel_to in self.admin_site._registry: # If the related object has an admin interface:
    226232            # TODO: "id_" is hard-coded here. This should instead use the correct
    227233            # 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);"> ' % \
    229235                (related_url, name))
    230236            output.append(u'<img src="%simg/admin/icon_addlink.gif" width="10" height="10" alt="%s"/></a>' % (settings.ADMIN_MEDIA_PREFIX, _('Add Another')))
    231237        return mark_safe(u''.join(output))
  • tests/regressiontests/admin_widgets/tests.py

    diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py
    index 4dc2930..ce5c78c 100644
    a b class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase): 
    110110        response = self.client.get("/widget_admin/admin_widgets/cartire/add/")
    111111        self.assert_("BMW M3" not in response.content)
    112112        self.assert_("Volkswagon Passat" in response.content)
     113
     114class 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/add/')
     125        self.failUnless('/widget_admin/auth/user/add/' in response.content)
  • tests/regressiontests/admin_widgets/widgetadmin.py

    diff --git a/tests/regressiontests/admin_widgets/widgetadmin.py b/tests/regressiontests/admin_widgets/widgetadmin.py
    index bc28dac..bd68954 100644
    a b import models 
    88class WidgetAdmin(admin.AdminSite):
    99    pass
    1010
     11class CarAdmin(admin.ModelAdmin):
     12    list_display = ['make', 'model', 'owner']
     13    list_editable = ['owner']
    1114
    1215class CarTireAdmin(admin.ModelAdmin):
    1316    def formfield_for_foreignkey(self, db_field, request, **kwargs):
    class CarTireAdmin(admin.ModelAdmin): 
    1821
    1922site = WidgetAdmin()
    2023
    21 site.register(models.Car)
     24site.register(models.User)
     25site.register(models.Car, CarAdmin)
    2226site.register(models.CarTire, CarTireAdmin)
Back to Top