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 /django/contrib/admin/widgets.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 'django/contrib/admin/widgets.py')
| -rw-r--r-- | django/contrib/admin/widgets.py | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index 1f438daf2d..aeb74773ac 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -428,7 +428,9 @@ class AutocompleteMixin: } if not self.is_required and not self.allow_multiple_selected: default[1].append(self.create_option(name, '', '', False, 0)) - to_field_name = getattr(self.field.remote_field, 'field_name', self.field.model._meta.pk.name) + remote_model_opts = self.field.remote_field.model._meta + to_field_name = getattr(self.field.remote_field, 'field_name', remote_model_opts.pk.attname) + to_field_name = remote_model_opts.get_field(to_field_name).attname choices = ( (getattr(obj, to_field_name), self.choices.field.label_from_instance(obj)) for obj in self.choices.queryset.using(self.db).filter(**{'%s__in' % to_field_name: selected_choices}) |
