Fixed #32545 -- Improved admin widget for raw_id_fields for UUIDFields.

Co-Authored-By: Jerome Leclanche <jerome@leclan.ch>
This commit is contained in:
Shubh1815 2021-11-28 12:10:10 +05:30 committed by Mariusz Felisiak
parent ed2018037d
commit 05e29da421
3 changed files with 22 additions and 11 deletions

View File

@ -350,10 +350,6 @@ body.popup .submit-row {
width: 2.2em; width: 2.2em;
} }
.vTextField, .vUUIDField {
width: 20em;
}
.vIntegerField { .vIntegerField {
width: 5em; width: 5em;
} }
@ -366,6 +362,10 @@ body.popup .submit-row {
width: 5em; width: 5em;
} }
.vTextField, .vUUIDField {
width: 20em;
}
/* INLINES */ /* INLINES */
.inline-group { .inline-group {

View File

@ -8,7 +8,7 @@ from django import forms
from django.conf import settings from django.conf import settings
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import URLValidator 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 import reverse
from django.urls.exceptions import NoReverseMatch from django.urls.exceptions import NoReverseMatch
from django.utils.html import smart_urlquote from django.utils.html import smart_urlquote
@ -149,7 +149,10 @@ class ForeignKeyRawIdWidget(forms.TextInput):
context['related_url'] = related_url context['related_url'] = related_url
context['link_title'] = _('Lookup') context['link_title'] = _('Lookup')
# The JavaScript code looks for this class. # 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: else:
context['related_url'] = None context['related_url'] = None
if context['widget']['value']: if context['widget']['value']:

View File

@ -26,7 +26,7 @@ from django.utils import translation
from .models import ( from .models import (
Advisor, Album, Band, Bee, Car, Company, Event, Honeycomb, Individual, Advisor, Album, Band, Bee, Car, Company, Event, Honeycomb, Individual,
Inventory, Member, MyFileField, Profile, School, Student, Inventory, Member, MyFileField, Profile, ReleaseEvent, School, Student,
UnsafeLimitChoicesTo, VideoStream, UnsafeLimitChoicesTo, VideoStream,
) )
from .widgetadmin import site as widget_admin_site from .widgetadmin import site as widget_admin_site
@ -538,19 +538,27 @@ class ForeignKeyRawIdWidgetTest(TestCase):
band.album_set.create( band.album_set.create(
name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg' name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg'
) )
rel = Album._meta.get_field('band').remote_field rel_uuid = Album._meta.get_field('band').remote_field
w = widgets.ForeignKeyRawIdWidget(rel_uuid, widget_admin_site)
w = widgets.ForeignKeyRawIdWidget(rel, widget_admin_site)
self.assertHTMLEqual( self.assertHTMLEqual(
w.render('test', band.uuid, attrs={}), w.render('test', band.uuid, attrs={}),
'<input type="text" name="test" value="%(banduuid)s" ' '<input type="text" name="test" value="%(banduuid)s" '
'class="vForeignKeyRawIdAdminField">' 'class="vForeignKeyRawIdAdminField vUUIDField">'
'<a href="/admin_widgets/band/?_to_field=uuid" class="related-lookup" ' '<a href="/admin_widgets/band/?_to_field=uuid" class="related-lookup" '
'id="lookup_id_test" title="Lookup"></a>&nbsp;<strong>' 'id="lookup_id_test" title="Lookup"></a>&nbsp;<strong>'
'<a href="/admin_widgets/band/%(bandpk)s/change/">Linkin Park</a>' '<a href="/admin_widgets/band/%(bandpk)s/change/">Linkin Park</a>'
'</strong>' % {'banduuid': band.uuid, 'bandpk': band.pk} '</strong>' % {'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={}),
'<input type="text" name="test" class="vForeignKeyRawIdAdminField">'
'<a href="/admin_widgets/album/?_to_field=id" class="related-lookup" '
'id="lookup_id_test" title="Lookup"></a>',
)
def test_relations_to_non_primary_key(self): def test_relations_to_non_primary_key(self):
# ForeignKeyRawIdWidget works with fields which aren't related to # ForeignKeyRawIdWidget works with fields which aren't related to
# the model's primary key. # the model's primary key.