Skip to content

Commit

Permalink
Fixed #32545 -- Improved admin widget for raw_id_fields for UUIDFields.
Browse files Browse the repository at this point in the history
Co-Authored-By: Jerome Leclanche <jerome@leclan.ch>
  • Loading branch information
2 people authored and felixxm committed Nov 29, 2021
1 parent ed20180 commit 05e29da
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
8 changes: 4 additions & 4 deletions django/contrib/admin/static/admin/css/forms.css
Expand Up @@ -350,10 +350,6 @@ body.popup .submit-row {
width: 2.2em;
}

.vTextField, .vUUIDField {
width: 20em;
}

.vIntegerField {
width: 5em;
}
Expand All @@ -366,6 +362,10 @@ body.popup .submit-row {
width: 5em;
}

.vTextField, .vUUIDField {
width: 20em;
}

/* INLINES */

.inline-group {
Expand Down
7 changes: 5 additions & 2 deletions django/contrib/admin/widgets.py
Expand Up @@ -8,7 +8,7 @@
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
Expand Down Expand Up @@ -149,7 +149,10 @@ def get_context(self, name, value, attrs):
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']:
Expand Down
18 changes: 13 additions & 5 deletions tests/admin_widgets/tests.py
Expand Up @@ -26,7 +26,7 @@

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
Expand Down Expand Up @@ -538,19 +538,27 @@ def test_render(self):
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.
Expand Down

0 comments on commit 05e29da

Please sign in to comment.