Permalink
Browse files

Fixed #16542 -- Made Raw ID form widgets shipped with the admin app r…

…ender the related object lookup tool only when the related model is effectively registered with the AdminSite.

Also, converted these widgets to reverse named URLs instead of hard-coded '../../...'-style links, refs #15294.
Thanks Florian Apolloner for the initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16578 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 56775c2 commit 290d7d4d21692336aeb074917acbf9804275fbdd @ramiro ramiro committed Aug 1, 2011
@@ -154,7 +154,8 @@ def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
"""
db = kwargs.get('using')
if db_field.name in self.raw_id_fields:
- kwargs['widget'] = widgets.ForeignKeyRawIdWidget(db_field.rel, using=db)
+ kwargs['widget'] = widgets.ForeignKeyRawIdWidget(db_field.rel,
+ self.admin_site, using=db)
elif db_field.name in self.radio_fields:
kwargs['widget'] = widgets.AdminRadioSelect(attrs={
'class': get_ul_class(self.radio_fields[db_field.name]),
@@ -174,7 +175,8 @@ def formfield_for_manytomany(self, db_field, request=None, **kwargs):
db = kwargs.get('using')
if db_field.name in self.raw_id_fields:
- kwargs['widget'] = widgets.ManyToManyRawIdWidget(db_field.rel, using=db)
+ kwargs['widget'] = widgets.ManyToManyRawIdWidget(db_field.rel,
+ self.admin_site, using=db)
kwargs['help_text'] = ''
elif db_field.name in (list(self.filter_vertical) + list(self.filter_horizontal)):
kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical))
@@ -4,7 +4,7 @@
import copy
from django import forms
-from django.core.urlresolvers import reverse, NoReverseMatch
+from django.core.urlresolvers import reverse
from django.forms.widgets import RadioFieldRenderer
from django.forms.util import flatatt
from django.templatetags.static import static
@@ -112,29 +112,38 @@ class ForeignKeyRawIdWidget(forms.TextInput):
A Widget for displaying ForeignKeys in the "raw_id" interface rather than
in a <select> box.
"""
- def __init__(self, rel, attrs=None, using=None):
+ def __init__(self, rel, admin_site, attrs=None, using=None):
self.rel = rel
+ self.admin_site = admin_site
self.db = using
super(ForeignKeyRawIdWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
+ rel_to = self.rel.to
if attrs is None:
attrs = {}
- related_url = '../../../%s/%s/' % (self.rel.to._meta.app_label, self.rel.to._meta.object_name.lower())
- params = self.url_parameters()
- if params:
- url = u'?' + u'&amp;'.join([u'%s=%s' % (k, v) for k, v in params.items()])
- else:
- url = u''
- if "class" not in attrs:
- attrs['class'] = 'vForeignKeyRawIdAdminField' # The JavaScript looks for this hook.
- output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)]
- # TODO: "id_" is hard-coded here. This should instead use the correct
- # API to determine the ID dynamically.
- output.append(u'<a href="%s%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);"> '
- % (related_url, url, name))
- output.append(u'<img src="%s" width="16" height="16" alt="%s" /></a>'
- % (static('admin/img/selector-search.gif'), _('Lookup')))
+ extra = []
+ if rel_to in self.admin_site._registry:
+ # The related object is registered with the same AdminSite
+ related_url = reverse('admin:%s_%s_changelist' %
+ (rel_to._meta.app_label,
+ rel_to._meta.module_name),
+ current_app=self.admin_site.name)
+
+ params = self.url_parameters()
+ if params:
+ url = u'?' + u'&amp;'.join([u'%s=%s' % (k, v) for k, v in params.items()])
+ else:
+ url = u''
+ if "class" not in attrs:
+ attrs['class'] = 'vForeignKeyRawIdAdminField' # The JavaScript code looks for this hook.
+ # TODO: "lookup_id_" is hard-coded here. This should instead use
+ # the correct API to determine the ID dynamically.
+ extra.append(u'<a href="%s%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);"> '
+ % (related_url, url, name))
+ extra.append(u'<img src="%s" width="16" height="16" alt="%s" /></a>'
+ % (static('admin/img/selector-search.gif'), _('Lookup')))
+ output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)] + extra
if value:
output.append(self.label_for_value(value))
return mark_safe(u''.join(output))
@@ -164,7 +173,9 @@ class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
def render(self, name, value, attrs=None):
if attrs is None:
attrs = {}
- attrs['class'] = 'vManyToManyRawIdAdminField'
+ if self.rel.to in self.admin_site._registry:
+ # The related object is registered with the same AdminSite
+ attrs['class'] = 'vManyToManyRawIdAdminField'
if value:
value = ','.join([force_unicode(v) for v in value])
else:
@@ -232,8 +243,8 @@ def render(self, name, value, *args, **kwargs):
output = [self.widget.render(name, value, *args, **kwargs)]
if self.can_add_related:
related_url = reverse('admin:%s_%s_add' % info, current_app=self.admin_site.name)
- # TODO: "id_" is hard-coded here. This should instead use the correct
- # API to determine the ID dynamically.
+ # TODO: "add_id_" is hard-coded here. This should instead use the
+ # correct API to determine the ID dynamically.
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="%s" width="10" height="10" alt="%s"/></a>'
@@ -67,3 +67,35 @@ class CarTire(models.Model):
A single car tire. This to test that a user can only select their own cars.
"""
car = models.ForeignKey(Car)
+
+class Honeycomb(models.Model):
+ location = models.CharField(max_length=20)
+
+class Bee(models.Model):
+ """
+ A model with a FK to a model that won't be registered with the admin
+ (Honeycomb) so the corresponding raw ID widget won't have a magnifying
+ glass link to select related honeycomb instances.
+ """
+ honeycomb = models.ForeignKey(Honeycomb)
+
+class Individual(models.Model):
+ """
+ A model with a FK to itself. It won't be registered with the admin, so the
+ corresponding raw ID widget won't have a magnifying glass link to select
+ related instances (rendering will be called programmatically in this case).
+ """
+ name = models.CharField(max_length=20)
+ parent = models.ForeignKey('self', null=True)
+
+class Company(models.Model):
+ name = models.CharField(max_length=20)
+
+class Advisor(models.Model):
+ """
+ A model with a m2m to a model that won't be registered with the admin
+ (Company) so the corresponding raw ID widget won't have a magnifying
+ glass link to select related company instances.
+ """
+ name = models.CharField(max_length=20)
+ companies = models.ManyToManyField(Company)
Oops, something went wrong.

0 comments on commit 290d7d4

Please sign in to comment.