Fixed #10537: Fixed the foreign key add-another link in list_editable. Thanks, Alex and jezdez.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10235 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jacob Kaplan-Moss 2009-03-30 23:16:34 +00:00
parent 4e035a03b6
commit 4378af21b6
3 changed files with 26 additions and 3 deletions

View File

@ -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'<a href="%sadd/" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \
output.append(u'<a href="%s" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \
(related_url, name))
output.append(u'<img src="%simg/admin/icon_addlink.gif" width="10" height="10" alt="%s"/></a>' % (settings.ADMIN_MEDIA_PREFIX, _('Add Another')))
return mark_safe(u''.join(output))

View File

@ -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)

View File

@ -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)