Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #8787 -- Fixed failing admin_widgets tests. This fixes ManyToMa…

…nyRawIdWidget which was broken with [8823]. Thanks Alex Gaynor.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8846 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0cd5caa6a853bc12d6b09aba08e9d70f1d59c667 1 parent 67e141a
Brian Rosner brosner authored
30 django/contrib/admin/widgets.py
View
@@ -105,13 +105,12 @@ def __init__(self, rel, attrs=None):
super(ForeignKeyRawIdWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
- from django.contrib.admin.views.main import TO_FIELD_VAR
related_url = '../../../%s/%s/' % (self.rel.to._meta.app_label, self.rel.to._meta.object_name.lower())
- params = {}
- if self.rel.limit_choices_to:
- params.update(dict([(k, ','.join(v)) for k, v in self.rel.limit_choices_to.items()]))
- params.update({TO_FIELD_VAR: self.rel.get_related_field().name})
- url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in params.items()])
+ params = self.url_parameters()
+ if params:
+ url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in params.items()])
+ else:
+ url = ''
if not attrs.has_key('class'):
attrs['class'] = 'vForeignKeyRawIdAdminField' # The JavaScript looks for this hook.
output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)]
@@ -123,7 +122,19 @@ def render(self, name, value, attrs=None):
if value:
output.append(self.label_for_value(value))
return mark_safe(u''.join(output))
-
+
+ def base_url_parameters(self):
+ params = {}
+ if self.rel.limit_choices_to:
+ params.update(dict([(k, ','.join(v)) for k, v in self.rel.limit_choices_to.items()]))
+ return params
+
+ def url_parameters(self):
+ from django.contrib.admin.views.main import TO_FIELD_VAR
+ params = self.base_url_parameters()
+ params.update({TO_FIELD_VAR: self.rel.get_related_field().name})
+ return params
+
def label_for_value(self, value):
key = self.rel.get_related_field().name
obj = self.rel.to.objects.get(**{key: value})
@@ -144,7 +155,10 @@ def render(self, name, value, attrs=None):
else:
value = ''
return super(ManyToManyRawIdWidget, self).render(name, value, attrs)
-
+
+ def url_parameters(self):
+ return self.base_url_parameters()
+
def label_for_value(self, value):
return ''
4 tests/regressiontests/admin_widgets/models.py
View
@@ -69,7 +69,7 @@ def __unicode__(self):
>>> rel = Album._meta.get_field('band').rel
>>> w = ForeignKeyRawIdWidget(rel)
>>> print conditional_escape(w.render('test', band.pk, attrs={}))
-<input type="text" name="test" value="1" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Linkin Park</strong>
+<input type="text" name="test" value="1" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Linkin Park</strong>
>>> m1 = Member.objects.create(pk=1, name='Chester')
>>> m2 = Member.objects.create(pk=2, name='Mike')
@@ -100,7 +100,7 @@ def __unicode__(self):
>>> rel = Inventory._meta.get_field('parent').rel
>>> w = ForeignKeyRawIdWidget(rel)
>>> print w.render('test', core.parent_id, attrs={})
-<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="/admin_media/img/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>
+<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>
""" % {
'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
'STORAGE_URL': default_storage.url(''),
Please sign in to comment.
Something went wrong with that request. Please try again.