From a603e672dedf99df8d3f64d6123dcaf28cd8c2de Mon Sep 17 00:00:00 2001
From: Hugo Osvaldo Barrera <hugo@barrera.io>
Date: Wed, 27 Jan 2016 21:43:04 -0300
Subject: [PATCH] Add links when using raw_id_fields in admin
Closes #7923
---
django/contrib/admin/widgets.py | 19 ++++++++++++++++++-
tests/admin_widgets/tests.py | 9 ++++++---
2 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
index 3424e7f..a2df938 100644
a
|
b
|
from django.forms.utils import flatatt
|
11 | 11 | from django.forms.widgets import RadioFieldRenderer |
12 | 12 | from django.template.loader import render_to_string |
13 | 13 | from django.urls import reverse |
| 14 | from django.urls.exceptions import NoReverseMatch |
14 | 15 | from django.utils import six |
15 | 16 | from django.utils.encoding import force_text |
16 | 17 | from django.utils.html import ( |
… |
… |
class ForeignKeyRawIdWidget(forms.TextInput):
|
194 | 195 | key = self.rel.get_related_field().name |
195 | 196 | try: |
196 | 197 | obj = self.rel.model._default_manager.using(self.db).get(**{key: value}) |
197 | | return ' <strong>%s</strong>' % escape(Truncator(obj).words(14, truncate='...')) |
| 198 | change_url = reverse( |
| 199 | '%s:%s_%s_change' % ( |
| 200 | self.admin_site.name, |
| 201 | obj._meta.app_label, |
| 202 | obj._meta.object_name.lower(), |
| 203 | ), |
| 204 | args=(obj.pk,) |
| 205 | ) |
| 206 | return ' <strong><a href="%s">%s</a></strong>' % ( |
| 207 | change_url, |
| 208 | escape(Truncator(obj).words(14, truncate='...')), |
| 209 | ) |
| 210 | except NoReverseMatch: |
| 211 | # Admin not registered for target model: |
| 212 | return ' <strong>%s</strong>' % ( |
| 213 | escape(Truncator(obj).words(14, truncate='...')), |
| 214 | ) |
198 | 215 | except (ValueError, self.rel.model.DoesNotExist): |
199 | 216 | return '' |
200 | 217 | |
diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py
index cbaeaa8..ce79dde 100644
a
|
b
|
class ForeignKeyRawIdWidgetTest(TestCase):
|
480 | 480 | '<input type="text" name="test" value="%(bandpk)s" ' |
481 | 481 | 'class="vForeignKeyRawIdAdminField" />' |
482 | 482 | '<a href="/admin_widgets/band/?_to_field=id" class="related-lookup" ' |
483 | | 'id="lookup_id_test" title="Lookup"></a> <strong>Linkin Park</strong>' |
| 483 | 'id="lookup_id_test" title="Lookup"></a> <strong>' |
| 484 | '<a href="/admin_widgets/band/1/change/">Linkin Park</a></strong>' |
484 | 485 | % {'bandpk': band.pk} |
485 | 486 | ) |
486 | 487 | |
… |
… |
class ForeignKeyRawIdWidgetTest(TestCase):
|
500 | 501 | 'class="vForeignKeyRawIdAdminField" />' |
501 | 502 | '<a href="/admin_widgets/inventory/?_to_field=barcode" ' |
502 | 503 | 'class="related-lookup" id="lookup_id_test" title="Lookup"></a>' |
503 | | ' <strong>Apple</strong>' |
| 504 | ' <strong><a href="/admin_widgets/inventory/1/change/">' |
| 505 | 'Apple</a></strong>' |
504 | 506 | ) |
505 | 507 | |
506 | 508 | def test_fk_related_model_not_in_admin(self): |
… |
… |
class ForeignKeyRawIdWidgetTest(TestCase):
|
549 | 551 | '<input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" />' |
550 | 552 | '<a href="/admin_widgets/inventory/?_to_field=barcode" ' |
551 | 553 | 'class="related-lookup" id="lookup_id_test" title="Lookup"></a>' |
552 | | ' <strong>Hidden</strong>' |
| 554 | ' <strong><a href="/admin_widgets/inventory/1/change/">' |
| 555 | 'Hidden</a></strong>' |
553 | 556 | ) |
554 | 557 | |
555 | 558 | |