1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

newforms-admin: Fixed #4641. ForeignKeyRawIdWidget now properly displays the related object value. Thanks, Matthew Flanagan for the original patch.

git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7177 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Brian Rosner 2008-02-29 05:41:32 +00:00
parent 397199c577
commit 3d61eb4dae
2 changed files with 13 additions and 5 deletions

View File

@ -7,6 +7,7 @@ WIDGET_TESTS = """
>>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget
>>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
>>> from django.contrib.admin.models import LogEntry
>>> from django.contrib.auth.models import User
Calling conditional_escape on the output of widget.render will simulate what
happens in the template. This is easier than setting up a template and context
@ -29,10 +30,16 @@ HTML escaped.
>>> print conditional_escape(w.render('test', 'test'))
Currently: <a target="_blank" href="%(MEDIA_URL)stest">test</a> <br>Change: <input type="file" name="test" />
To test ForeignKeyRawIdWidget a user object must be created. Its pk is
explicitly set to 100 to avoid having to potentially overmatch in the test.
>>> user = User.objects.create(pk=100, username='jdoe')
>>> entry = LogEntry(action_flag=1, user=user)
>>> entry.save()
>>> rel = LogEntry._meta.get_field('user').rel
>>> w = ForeignKeyRawIdWidget(rel)
>>> print conditional_escape(w.render('test', 'test', attrs={}))
<input type="text" name="test" value="test" class="vForeignKeyRawIdAdminField" /><a href="../../../auth/user/" 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>
>>> print conditional_escape(w.render('test', entry.user.pk, attrs={}))
<input type="text" name="test" value="100" class="vForeignKeyRawIdAdminField" /><a href="../../../auth/user/" 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>jdoe</strong>
""" % {
'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,

View File

@ -4,7 +4,7 @@ Form Widget classes specific to the Django admin site.
from django import newforms as forms
from django.utils.datastructures import MultiValueDict
from django.utils.text import capfirst
from django.utils.text import capfirst, truncate_words
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe
from django.conf import settings
@ -100,9 +100,10 @@ class ForeignKeyRawIdWidget(forms.TextInput):
output.append('<a href="%s%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);"> ' % \
(related_url, url, name))
output.append('<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>' % settings.ADMIN_MEDIA_PREFIX)
if value:
output.append('&nbsp;<strong>%s</strong>' % \
truncate_words(self.rel.to.objects.get(pk=value), 14))
return mark_safe(u''.join(output))
#if self.change: # TODO
#output.append('&nbsp;<strong>TODO</strong>')
class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
"""