diff options
| author | Johannes Maron <info@johanneshoppe.com> | 2021-03-18 11:21:23 +0100 |
|---|---|---|
| committer | Carlton Gibson <carlton.gibson@noumenal.es> | 2021-03-18 14:21:12 +0100 |
| commit | a8fef6daaf75cd24262d973b154fb6580efd99a4 (patch) | |
| tree | e70b7be0a7e5fd69f2edef03787dab806c1bde4d /tests/admin_widgets/test_autocomplete_widget.py | |
| parent | 6b020f3c94fb7f27875d5fe21a71a5ee7c9a7538 (diff) | |
[3.2.x] 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.
Backport of ceb4b9ee68dffc6ab0398886f1758f15f037c472 from main Backport of 03d0f12c823239812da21e5180aaa74dc6fd146e from main
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>
Diffstat (limited to 'tests/admin_widgets/test_autocomplete_widget.py')
| -rw-r--r-- | tests/admin_widgets/test_autocomplete_widget.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/tests/admin_widgets/test_autocomplete_widget.py b/tests/admin_widgets/test_autocomplete_widget.py index d8ee7e9f3a..279acfe615 100644 --- a/tests/admin_widgets/test_autocomplete_widget.py +++ b/tests/admin_widgets/test_autocomplete_widget.py @@ -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 = ( |
