Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Brian Rosner authored February 29, 2008
11  django/contrib/admin/tests/widgets.py
@@ -7,6 +7,7 @@
7 7
 >>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget
8 8
 >>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
9 9
 >>> from django.contrib.admin.models import LogEntry
  10
+>>> from django.contrib.auth.models import User
10 11
 
11 12
 Calling conditional_escape on the output of widget.render will simulate what
12 13
 happens in the template. This is easier than setting up a template and context
@@ -29,10 +30,16 @@
29 30
 >>> print conditional_escape(w.render('test', 'test'))
30 31
 Currently: <a target="_blank" href="%(MEDIA_URL)stest">test</a> <br>Change: <input type="file" name="test" />
31 32
 
  33
+To test ForeignKeyRawIdWidget a user object must be created. Its pk is
  34
+explicitly set to 100 to avoid having to potentially overmatch in the test.
  35
+
  36
+>>> user = User.objects.create(pk=100, username='jdoe')
  37
+>>> entry = LogEntry(action_flag=1, user=user)
  38
+>>> entry.save()
32 39
 >>> rel = LogEntry._meta.get_field('user').rel
33 40
 >>> w = ForeignKeyRawIdWidget(rel)
34  
->>> print conditional_escape(w.render('test', 'test', attrs={}))
35  
-<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>
  41
+>>> print conditional_escape(w.render('test', entry.user.pk, attrs={}))
  42
+<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>
36 43
 
37 44
 """ % {
38 45
     'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
7  django/contrib/admin/widgets.py
@@ -4,7 +4,7 @@
4 4
 
5 5
 from django import newforms as forms
6 6
 from django.utils.datastructures import MultiValueDict
7  
-from django.utils.text import capfirst
  7
+from django.utils.text import capfirst, truncate_words
8 8
 from django.utils.translation import ugettext as _
9 9
 from django.utils.safestring import mark_safe
10 10
 from django.conf import settings
@@ -100,9 +100,10 @@ def render(self, name, value, attrs=None):
100 100
         output.append('<a href="%s%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);"> ' % \
101 101
             (related_url, url, name))
102 102
         output.append('<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>' % settings.ADMIN_MEDIA_PREFIX)
  103
+        if value:
  104
+            output.append('&nbsp;<strong>%s</strong>' % \
  105
+                truncate_words(self.rel.to.objects.get(pk=value), 14))
103 106
         return mark_safe(u''.join(output))
104  
-        #if self.change: # TODO
105  
-            #output.append('&nbsp;<strong>TODO</strong>')
106 107
             
107 108
 class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
108 109
     """

0 notes on commit 3d61eb4

Please sign in to comment.
Something went wrong with that request. Please try again.