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;
}
.vTextField, .vUUIDField {
width: 20em;
}
.vIntegerField {
width: 5em;
}
@ -366,6 +362,10 @@ body.popup .submit-row {
width: 5em;
}
.vTextField, .vUUIDField {
width: 20em;
}
/* INLINES */
.inline-group {

View File

@ -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']:

View File

@ -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={}),
'<input type="text" name="test" value="%(banduuid)s" '
'class="vForeignKeyRawIdAdminField">'
'class="vForeignKeyRawIdAdminField vUUIDField">'
'<a href="/admin_widgets/band/?_to_field=uuid" class="related-lookup" '
'id="lookup_id_test" title="Lookup"></a>&nbsp;<strong>'
'<a href="/admin_widgets/band/%(bandpk)s/change/">Linkin Park</a>'
'</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):
# ForeignKeyRawIdWidget works with fields which aren't related to
# the model's primary key.