Fixed #8787 -- Fixed failing admin_widgets tests. This fixes ManyToManyRawIdWidget which was broken with [8823]. Thanks Alex Gaynor.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8846 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Brian Rosner 2008-09-02 06:10:14 +00:00
parent 67e141a7cd
commit 0cd5caa6a8
2 changed files with 24 additions and 10 deletions

View File

@ -105,13 +105,12 @@ class ForeignKeyRawIdWidget(forms.TextInput):
super(ForeignKeyRawIdWidget, self).__init__(attrs) super(ForeignKeyRawIdWidget, self).__init__(attrs)
def render(self, name, value, attrs=None): 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()) related_url = '../../../%s/%s/' % (self.rel.to._meta.app_label, self.rel.to._meta.object_name.lower())
params = {} params = self.url_parameters()
if self.rel.limit_choices_to: if params:
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()]) url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in params.items()])
else:
url = ''
if not attrs.has_key('class'): if not attrs.has_key('class'):
attrs['class'] = 'vForeignKeyRawIdAdminField' # The JavaScript looks for this hook. attrs['class'] = 'vForeignKeyRawIdAdminField' # The JavaScript looks for this hook.
output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)] output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)]
@ -124,6 +123,18 @@ class ForeignKeyRawIdWidget(forms.TextInput):
output.append(self.label_for_value(value)) output.append(self.label_for_value(value))
return mark_safe(u''.join(output)) 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): def label_for_value(self, value):
key = self.rel.get_related_field().name key = self.rel.get_related_field().name
obj = self.rel.to.objects.get(**{key: value}) obj = self.rel.to.objects.get(**{key: value})
@ -145,6 +156,9 @@ class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
value = '' value = ''
return super(ManyToManyRawIdWidget, self).render(name, value, attrs) return super(ManyToManyRawIdWidget, self).render(name, value, attrs)
def url_parameters(self):
return self.base_url_parameters()
def label_for_value(self, value): def label_for_value(self, value):
return '' return ''

View File

@ -69,7 +69,7 @@ Currently: <a target="_blank" href="%(STORAGE_URL)salbums/hybrid_theory.jpg">alb
>>> rel = Album._meta.get_field('band').rel >>> rel = Album._meta.get_field('band').rel
>>> w = ForeignKeyRawIdWidget(rel) >>> w = ForeignKeyRawIdWidget(rel)
>>> print conditional_escape(w.render('test', band.pk, attrs={})) >>> 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') >>> m1 = Member.objects.create(pk=1, name='Chester')
>>> m2 = Member.objects.create(pk=2, name='Mike') >>> m2 = Member.objects.create(pk=2, name='Mike')
@ -100,7 +100,7 @@ True
>>> rel = Inventory._meta.get_field('parent').rel >>> rel = Inventory._meta.get_field('parent').rel
>>> w = ForeignKeyRawIdWidget(rel) >>> w = ForeignKeyRawIdWidget(rel)
>>> print w.render('test', core.parent_id, attrs={}) >>> 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, 'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
'STORAGE_URL': default_storage.url(''), 'STORAGE_URL': default_storage.url(''),