1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

newforms-admin: Fixed #4491. Thanks Honza Kral.

git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@6074 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Joseph Kocherhans 2007-09-09 19:15:01 +00:00
parent 4ccc5ff3e1
commit c05527dd60
2 changed files with 5 additions and 3 deletions

View File

@ -157,7 +157,7 @@ class BaseModelAdmin(object):
# Wrap the widget's render() method with a method that adds # Wrap the widget's render() method with a method that adds
# extra HTML to the end of the rendered output. # extra HTML to the end of the rendered output.
formfield = db_field.formfield(**kwargs) formfield = db_field.formfield(**kwargs)
formfield.widget.render = widgets.RelatedFieldWidgetWrapper(formfield.widget.render, db_field.rel) formfield.widget.render = widgets.RelatedFieldWidgetWrapper(formfield.widget.render, db_field.rel, self.admin_site)
return formfield return formfield
# For any other type of field, just call its formfield() method. # For any other type of field, just call its formfield() method.

View File

@ -91,15 +91,17 @@ class RelatedFieldWidgetWrapper(object):
This class is a wrapper whose __call__() method mimics the interface of a This class is a wrapper whose __call__() method mimics the interface of a
Widget's render() method. Widget's render() method.
""" """
def __init__(self, render_func, rel): def __init__(self, render_func, rel, admin_site):
self.render_func, self.rel = render_func, rel self.render_func, self.rel = render_func, rel
# so we can check if the related object is registered with this AdminSite
self.admin_site = admin_site
def __call__(self, name, value, *args, **kwargs): def __call__(self, name, value, *args, **kwargs):
from django.conf import settings from django.conf import settings
rel_to = self.rel.to rel_to = self.rel.to
related_url = '../../../%s/%s/' % (rel_to._meta.app_label, rel_to._meta.object_name.lower()) related_url = '../../../%s/%s/' % (rel_to._meta.app_label, rel_to._meta.object_name.lower())
output = [self.render_func(name, value, *args, **kwargs)] output = [self.render_func(name, value, *args, **kwargs)]
if rel_to._meta.admin: # If the related object has an admin interface: 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 # TODO: "id_" is hard-coded here. This should instead use the correct
# API to determine the ID dynamically. # 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="%sadd/" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \