Ticket #7923: 0001-Add-links-when-using-raw_id_fields-in-admin.patch

File 0001-Add-links-when-using-raw_id_fields-in-admin.patch, 3.6 KB (added by Hugo Osvaldo Barrera, 9 years ago)

Patch updated to apply on current master

  • django/contrib/admin/widgets.py

    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  
    1111from django.forms.widgets import RadioFieldRenderer
    1212from django.template.loader import render_to_string
    1313from django.urls import reverse
     14from django.urls.exceptions import NoReverseMatch
    1415from django.utils import six
    1516from django.utils.encoding import force_text
    1617from django.utils.html import (
    class ForeignKeyRawIdWidget(forms.TextInput):  
    194195        key = self.rel.get_related_field().name
    195196        try:
    196197            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
    197             return '&nbsp;<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 '&nbsp;<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 '&nbsp;<strong>%s</strong>' % (
     213                escape(Truncator(obj).words(14, truncate='...')),
     214            )
    198215        except (ValueError, self.rel.model.DoesNotExist):
    199216            return ''
    200217
  • tests/admin_widgets/tests.py

    diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py
    index cbaeaa8..ce79dde 100644
    a b class ForeignKeyRawIdWidgetTest(TestCase):  
    480480            '<input type="text" name="test" value="%(bandpk)s" '
    481481            'class="vForeignKeyRawIdAdminField" />'
    482482            '<a href="/admin_widgets/band/?_to_field=id" class="related-lookup" '
    483             'id="lookup_id_test" title="Lookup"></a>&nbsp;<strong>Linkin Park</strong>'
     483            'id="lookup_id_test" title="Lookup"></a>&nbsp;<strong>'
     484            '<a href="/admin_widgets/band/1/change/">Linkin Park</a></strong>'
    484485            % {'bandpk': band.pk}
    485486        )
    486487
    class ForeignKeyRawIdWidgetTest(TestCase):  
    500501            'class="vForeignKeyRawIdAdminField" />'
    501502            '<a href="/admin_widgets/inventory/?_to_field=barcode" '
    502503            'class="related-lookup" id="lookup_id_test" title="Lookup"></a>'
    503             '&nbsp;<strong>Apple</strong>'
     504            '&nbsp;<strong><a href="/admin_widgets/inventory/1/change/">'
     505            'Apple</a></strong>'
    504506        )
    505507
    506508    def test_fk_related_model_not_in_admin(self):
    class ForeignKeyRawIdWidgetTest(TestCase):  
    549551            '<input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" />'
    550552            '<a href="/admin_widgets/inventory/?_to_field=barcode" '
    551553            'class="related-lookup" id="lookup_id_test" title="Lookup"></a>'
    552             '&nbsp;<strong>Hidden</strong>'
     554            '&nbsp;<strong><a href="/admin_widgets/inventory/1/change/">'
     555            'Hidden</a></strong>'
    553556        )
    554557
    555558
Back to Top