From 8bf8d0e0ecc1805480deb94feb4675b09d3b3a95 Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Wed, 27 Jan 2016 21:43:04 -0300 Subject: [PATCH] Fixed #7923 -- Added links to objects displayed by ModelAdmin.raw_id_fields. --- AUTHORS | 1 + django/contrib/admin/widgets.py | 24 ++++++++++++++++++++---- docs/releases/1.10.txt | 3 +++ tests/admin_widgets/tests.py | 11 +++++++---- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/AUTHORS b/AUTHORS index 4e7aab8a86..4d9fcaecf4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -288,6 +288,7 @@ answer newbie questions, and generally made Django that much better: Hiroki Kiyohara Honza Král Horst Gutmann + Hugo Osvaldo Barrera Hyun Mi Ae Iacopo Spalletti Ian A Wilson diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index 3424e7f0ab..0b66c7de1d 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -11,11 +11,10 @@ from django.forms.utils import flatatt from django.forms.widgets import RadioFieldRenderer from django.template.loader import render_to_string from django.urls import reverse +from django.urls.exceptions import NoReverseMatch from django.utils import six from django.utils.encoding import force_text -from django.utils.html import ( - escape, format_html, format_html_join, smart_urlquote, -) +from django.utils.html import format_html, format_html_join, smart_urlquote from django.utils.safestring import mark_safe from django.utils.text import Truncator from django.utils.translation import ugettext as _ @@ -194,10 +193,27 @@ class ForeignKeyRawIdWidget(forms.TextInput): key = self.rel.get_related_field().name try: obj = self.rel.model._default_manager.using(self.db).get(**{key: value}) - return ' %s' % escape(Truncator(obj).words(14, truncate='...')) except (ValueError, self.rel.model.DoesNotExist): return '' + label = ' {}' + text = Truncator(obj).words(14, truncate='...') + try: + change_url = reverse( + '%s:%s_%s_change' % ( + self.admin_site.name, + obj._meta.app_label, + obj._meta.object_name.lower(), + ), + args=(obj.pk,) + ) + except NoReverseMatch: + pass # Admin not registered for target model. + else: + text = format_html('{}', change_url, text) + + return format_html(label, text) + class ManyToManyRawIdWidget(ForeignKeyRawIdWidget): """ diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index 6568d0ff45..a0419b76bc 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -56,6 +56,9 @@ Minor features using the current active language. A new ``LogEntry.get_change_message()`` method is now the preferred way of retrieving the change message. +* Selected objects for fields in ``ModelAdmin.raw_id_fields`` now have a link + to object's change form. + :mod:`django.contrib.admindocs` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py index 00f0f31b68..de27f0163b 100644 --- a/tests/admin_widgets/tests.py +++ b/tests/admin_widgets/tests.py @@ -480,8 +480,9 @@ class ForeignKeyRawIdWidgetTest(TestCase): '' ' Linkin Park' - % {'bandpk': band.pk} + 'id="lookup_id_test" title="Lookup"> ' + 'Linkin Park' + '' % {'bandpk': band.pk} ) def test_relations_to_non_primary_key(self): @@ -500,7 +501,8 @@ class ForeignKeyRawIdWidgetTest(TestCase): 'class="vForeignKeyRawIdAdminField" />' '' - ' Apple' + ' ' + 'Apple' % {'pk': apple.pk} ) def test_fk_related_model_not_in_admin(self): @@ -549,7 +551,8 @@ class ForeignKeyRawIdWidgetTest(TestCase): '' '' - ' Hidden' + ' ' + 'Hidden' % {'pk': hidden.pk} )