From 05e29da4212fa9f590d7bd10767ebacb25acfde9 Mon Sep 17 00:00:00 2001 From: Shubh1815 Date: Sun, 28 Nov 2021 12:10:10 +0530 Subject: [PATCH] Fixed #32545 -- Improved admin widget for raw_id_fields for UUIDFields. Co-Authored-By: Jerome Leclanche --- .../contrib/admin/static/admin/css/forms.css | 8 ++++---- django/contrib/admin/widgets.py | 7 +++++-- tests/admin_widgets/tests.py | 18 +++++++++++++----- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/django/contrib/admin/static/admin/css/forms.css b/django/contrib/admin/static/admin/css/forms.css index 5007d45bca..abcc0454f1 100644 --- a/django/contrib/admin/static/admin/css/forms.css +++ b/django/contrib/admin/static/admin/css/forms.css @@ -350,10 +350,6 @@ body.popup .submit-row { width: 2.2em; } -.vTextField, .vUUIDField { - width: 20em; -} - .vIntegerField { width: 5em; } @@ -366,6 +362,10 @@ body.popup .submit-row { width: 5em; } +.vTextField, .vUUIDField { + width: 20em; +} + /* INLINES */ .inline-group { diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index f1002cac6c..ec0423b284 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -8,7 +8,7 @@ from django import forms from django.conf import settings from django.core.exceptions import ValidationError from django.core.validators import URLValidator -from django.db.models import CASCADE +from django.db.models import CASCADE, UUIDField from django.urls import reverse from django.urls.exceptions import NoReverseMatch from django.utils.html import smart_urlquote @@ -149,7 +149,10 @@ class ForeignKeyRawIdWidget(forms.TextInput): context['related_url'] = related_url context['link_title'] = _('Lookup') # The JavaScript code looks for this class. - context['widget']['attrs'].setdefault('class', 'vForeignKeyRawIdAdminField') + css_class = 'vForeignKeyRawIdAdminField' + if isinstance(self.rel.get_related_field(), UUIDField): + css_class += ' vUUIDField' + context['widget']['attrs'].setdefault('class', css_class) else: context['related_url'] = None if context['widget']['value']: diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py index 133be0a5d2..677ee18cff 100644 --- a/tests/admin_widgets/tests.py +++ b/tests/admin_widgets/tests.py @@ -26,7 +26,7 @@ from django.utils import translation from .models import ( Advisor, Album, Band, Bee, Car, Company, Event, Honeycomb, Individual, - Inventory, Member, MyFileField, Profile, School, Student, + Inventory, Member, MyFileField, Profile, ReleaseEvent, School, Student, UnsafeLimitChoicesTo, VideoStream, ) from .widgetadmin import site as widget_admin_site @@ -538,19 +538,27 @@ class ForeignKeyRawIdWidgetTest(TestCase): band.album_set.create( name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg' ) - rel = Album._meta.get_field('band').remote_field - - w = widgets.ForeignKeyRawIdWidget(rel, widget_admin_site) + rel_uuid = Album._meta.get_field('band').remote_field + w = widgets.ForeignKeyRawIdWidget(rel_uuid, widget_admin_site) self.assertHTMLEqual( w.render('test', band.uuid, attrs={}), '' + 'class="vForeignKeyRawIdAdminField vUUIDField">' ' ' 'Linkin Park' '' % {'banduuid': band.uuid, 'bandpk': band.pk} ) + rel_id = ReleaseEvent._meta.get_field('album').remote_field + w = widgets.ForeignKeyRawIdWidget(rel_id, widget_admin_site) + self.assertHTMLEqual( + w.render('test', None, attrs={}), + '' + '', + ) + def test_relations_to_non_primary_key(self): # ForeignKeyRawIdWidget works with fields which aren't related to # the model's primary key.