Code

Ticket #4641: 4641_raw_admin_fields_display_fix.diff

File 4641_raw_admin_fields_display_fix.diff, 3.2 KB (added by brosner, 6 years ago)

more than likely the final patch to be committed. posted here to get some last minute feedback.

Line 
1diff --git a/django/contrib/admin/tests/widgets.py b/django/contrib/admin/tests/widgets.py
2index 0b20441..55d845c 100644
3--- a/django/contrib/admin/tests/widgets.py
4+++ b/django/contrib/admin/tests/widgets.py
5@@ -7,6 +7,7 @@ WIDGET_TESTS = """
6 >>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget
7 >>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
8 >>> from django.contrib.admin.models import LogEntry
9+>>> from django.contrib.auth.models import User
10 
11 Calling conditional_escape on the output of widget.render will simulate what
12 happens in the template. This is easier than setting up a template and context
13@@ -29,10 +30,13 @@ HTML escaped.
14 >>> print conditional_escape(w.render('test', 'test'))
15 Currently: <a target="_blank" href="%(MEDIA_URL)stest">test</a> <br>Change: <input type="file" name="test" />
16 
17+>>> entry = LogEntry(action_flag=1)
18+>>> entry.user = User.objects.create_user("jdoe", "jdoe@example.com")
19+>>> entry.save()
20 >>> rel = LogEntry._meta.get_field('user').rel
21 >>> w = ForeignKeyRawIdWidget(rel)
22->>> print conditional_escape(w.render('test', 'test', attrs={}))
23-<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>
24+>>> print conditional_escape(w.render('test', entry.user.pk, attrs={}))
25+<input type="text" name="test" value="..." 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>
26 
27 """ % {
28     'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
29diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
30index 26aacf7..f5c14e6 100644
31--- a/django/contrib/admin/widgets.py
32+++ b/django/contrib/admin/widgets.py
33@@ -4,7 +4,7 @@ Form Widget classes specific to the Django admin site.
34 
35 from django import newforms as forms
36 from django.utils.datastructures import MultiValueDict
37-from django.utils.text import capfirst
38+from django.utils.text import capfirst, truncate_words
39 from django.utils.translation import ugettext as _
40 from django.utils.safestring import mark_safe
41 from django.conf import settings
42@@ -100,9 +100,10 @@ class ForeignKeyRawIdWidget(forms.TextInput):
43         output.append('<a href="%s%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);"> ' % \
44             (related_url, url, name))
45         output.append('<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>' % settings.ADMIN_MEDIA_PREFIX)
46+        if value:
47+            output.append('&nbsp;<strong>%s</strong>' % \
48+                truncate_words(self.rel.to.objects.get(pk=value), 14))
49         return mark_safe(u''.join(output))
50-        #if self.change: # TODO
51-            #output.append('&nbsp;<strong>TODO</strong>')
52             
53 class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
54     """