1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #29010, Fixed #29138 -- Added limit_choices_to and to_field support to autocomplete fields.

* Fixed #29010 -- Added limit_choices_to support to autocomplete fields.
* Fixed #29138 -- Allowed autocomplete fields to target a custom
  to_field rather than the PK.
This commit is contained in:
Johannes Maron
2021-01-12 11:37:38 +01:00
committed by GitHub
parent ba3fb2e4d0
commit 3071660acf
11 changed files with 200 additions and 60 deletions

View File

@@ -14,12 +14,12 @@ class AlbumForm(forms.ModelForm):
fields = ['band', 'featuring']
widgets = {
'band': AutocompleteSelect(
Album._meta.get_field('band').remote_field,
Album._meta.get_field('band'),
admin.site,
attrs={'class': 'my-class'},
),
'featuring': AutocompleteSelect(
Album._meta.get_field('featuring').remote_field,
Album._meta.get_field('featuring'),
admin.site,
)
}
@@ -54,9 +54,12 @@ class AutocompleteMixinTests(TestCase):
'data-ajax--cache': 'true',
'data-ajax--delay': 250,
'data-ajax--type': 'GET',
'data-ajax--url': '/admin_widgets/band/autocomplete/',
'data-ajax--url': '/autocomplete/',
'data-theme': 'admin-autocomplete',
'data-allow-clear': 'false',
'data-app-label': 'admin_widgets',
'data-field-name': 'band',
'data-model-name': 'album',
'data-placeholder': ''
})
@@ -76,19 +79,19 @@ class AutocompleteMixinTests(TestCase):
self.assertJSONEqual(attrs['data-allow-clear'], False)
def test_get_url(self):
rel = Album._meta.get_field('band').remote_field
rel = Album._meta.get_field('band')
w = AutocompleteSelect(rel, admin.site)
url = w.get_url()
self.assertEqual(url, '/admin_widgets/band/autocomplete/')
self.assertEqual(url, '/autocomplete/')
def test_render_options(self):
beatles = Band.objects.create(name='The Beatles', style='rock')
who = Band.objects.create(name='The Who', style='rock')
# With 'band', a ForeignKey.
form = AlbumForm(initial={'band': beatles.pk})
form = AlbumForm(initial={'band': beatles.uuid})
output = form.as_table()
selected_option = '<option value="%s" selected>The Beatles</option>' % beatles.pk
option = '<option value="%s">The Who</option>' % who.pk
selected_option = '<option value="%s" selected>The Beatles</option>' % beatles.uuid
option = '<option value="%s">The Who</option>' % who.uuid
self.assertIn(selected_option, output)
self.assertNotIn(option, output)
# With 'featuring', a ManyToManyField.