mirror of
https://github.com/django/django.git
synced 2025-07-30 16:44:30 +00:00
Fixed #32466 -- Corrected autocomplete to_field resolution for complex cases.
In MTI or ForeignKey as primary key cases, it is required to fetch the attname from the field instance on the remote model in order to reliably resolve the to_field_name. Co-authored-by: Johannes Maron <info@johanneshoppe.com> Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com> Co-authored-by: Carlton Gibson <carlton.gibson@noumenal.es>
This commit is contained in:
parent
0b120f5d64
commit
03d0f12c82
8 changed files with 153 additions and 8 deletions
|
@ -5,7 +5,7 @@ from django.forms import ModelChoiceField
|
|||
from django.test import TestCase, override_settings
|
||||
from django.utils import translation
|
||||
|
||||
from .models import Album, Band
|
||||
from .models import Album, Band, ReleaseEvent, VideoStream
|
||||
|
||||
|
||||
class AlbumForm(forms.ModelForm):
|
||||
|
@ -41,6 +41,18 @@ class RequiredBandForm(forms.Form):
|
|||
)
|
||||
|
||||
|
||||
class VideoStreamForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = VideoStream
|
||||
fields = ['release_event']
|
||||
widgets = {
|
||||
'release_event': AutocompleteSelect(
|
||||
VideoStream._meta.get_field('release_event'),
|
||||
admin.site,
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF='admin_widgets.urls')
|
||||
class AutocompleteMixinTests(TestCase):
|
||||
empty_option = '<option value=""></option>'
|
||||
|
@ -114,6 +126,15 @@ class AutocompleteMixinTests(TestCase):
|
|||
output = form.as_table()
|
||||
self.assertNotIn(self.empty_option, output)
|
||||
|
||||
def test_render_options_fk_as_pk(self):
|
||||
beatles = Band.objects.create(name='The Beatles', style='rock')
|
||||
rubber_soul = Album.objects.create(name='Rubber Soul', band=beatles)
|
||||
release_event = ReleaseEvent.objects.create(name='Test Target', album=rubber_soul)
|
||||
form = VideoStreamForm(initial={'release_event': release_event.pk})
|
||||
output = form.as_table()
|
||||
selected_option = '<option value="%s" selected>Test Target</option>' % release_event.pk
|
||||
self.assertIn(selected_option, output)
|
||||
|
||||
def test_media(self):
|
||||
rel = Album._meta.get_field('band').remote_field
|
||||
base_files = (
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue