Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

newforms-admin: Fixed #4641. ForeignKeyRawIdWidget now properly displ…

…ays 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
  • Loading branch information...
commit 3d61eb4dae02998eed6a2d9194906959a0b91bec 1 parent 397199c
@brosner brosner authored
View
11 django/contrib/admin/tests/widgets.py
@@ -7,6 +7,7 @@
>>> 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 @@
>>> 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
7 django/contrib/admin/widgets.py
@@ -4,7 +4,7 @@
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 @@ def render(self, name, value, attrs=None):
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):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.