1
0
mirror of https://github.com/django/django.git synced 2025-10-29 16:46:11 +00:00

[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 ceb4b9ee68 from main Backport of 03d0f12c82 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>
This commit is contained in:
Johannes Maron
2021-03-18 11:21:23 +01:00
committed by Carlton Gibson
parent 6b020f3c94
commit a8fef6daaf
8 changed files with 178 additions and 8 deletions

View File

@@ -12,7 +12,10 @@ from django.test import RequestFactory, override_settings
from django.urls import reverse, reverse_lazy
from .admin import AnswerAdmin, QuestionAdmin
from .models import Answer, Author, Authorship, Book, Question
from .models import (
Answer, Author, Authorship, Bonus, Book, Employee, Manager, Parent,
PKChild, Question, Toy, WorkHour,
)
from .tests import AdminViewBasicTestCase
PAGINATOR_SIZE = AutocompleteJsonView.paginate_by
@@ -37,6 +40,12 @@ site.register(Question, QuestionAdmin)
site.register(Answer, AnswerAdmin)
site.register(Author, AuthorAdmin)
site.register(Book, BookAdmin)
site.register(Employee, search_fields=['name'])
site.register(WorkHour, autocomplete_fields=['employee'])
site.register(Manager, search_fields=['name'])
site.register(Bonus, autocomplete_fields=['recipient'])
site.register(PKChild, search_fields=['name'])
site.register(Toy, autocomplete_fields=['child'])
@contextmanager
@@ -94,6 +103,75 @@ class AutocompleteJsonViewTests(AdminViewBasicTestCase):
'pagination': {'more': False},
})
def test_custom_to_field_permission_denied(self):
Question.objects.create(question='Is this a question?')
request = self.factory.get(self.url, {'term': 'is', **self.opts, 'field_name': 'question_with_to_field'})
request.user = self.user
with self.assertRaises(PermissionDenied):
AutocompleteJsonView.as_view(**self.as_view_args)(request)
def test_custom_to_field_custom_pk(self):
q = Question.objects.create(question='Is this a question?')
opts = {
'app_label': Question._meta.app_label,
'model_name': Question._meta.model_name,
'field_name': 'related_questions',
}
request = self.factory.get(self.url, {'term': 'is', **opts})
request.user = self.superuser
response = AutocompleteJsonView.as_view(**self.as_view_args)(request)
self.assertEqual(response.status_code, 200)
data = json.loads(response.content.decode('utf-8'))
self.assertEqual(data, {
'results': [{'id': str(q.big_id), 'text': q.question}],
'pagination': {'more': False},
})
def test_to_field_resolution_with_mti(self):
"""
to_field resolution should correctly resolve for target models using
MTI. Tests for single and multi-level cases.
"""
tests = [
(Employee, WorkHour, 'employee'),
(Manager, Bonus, 'recipient'),
]
for Target, Remote, related_name in tests:
with self.subTest(target_model=Target, remote_model=Remote, related_name=related_name):
o = Target.objects.create(name="Frida Kahlo", gender=2, code="painter", alive=False)
opts = {
'app_label': Remote._meta.app_label,
'model_name': Remote._meta.model_name,
'field_name': related_name,
}
request = self.factory.get(self.url, {'term': 'frida', **opts})
request.user = self.superuser
response = AutocompleteJsonView.as_view(**self.as_view_args)(request)
self.assertEqual(response.status_code, 200)
data = json.loads(response.content.decode('utf-8'))
self.assertEqual(data, {
'results': [{'id': str(o.pk), 'text': o.name}],
'pagination': {'more': False},
})
def test_to_field_resolution_with_fk_pk(self):
p = Parent.objects.create(name="Bertie")
c = PKChild.objects.create(parent=p, name="Anna")
opts = {
'app_label': Toy._meta.app_label,
'model_name': Toy._meta.model_name,
'field_name': 'child',
}
request = self.factory.get(self.url, {'term': 'anna', **opts})
request.user = self.superuser
response = AutocompleteJsonView.as_view(**self.as_view_args)(request)
self.assertEqual(response.status_code, 200)
data = json.loads(response.content.decode('utf-8'))
self.assertEqual(data, {
'results': [{'id': str(c.pk), 'text': c.name}],
'pagination': {'more': False},
})
def test_field_does_not_exist(self):
request = self.factory.get(self.url, {'term': 'is', **self.opts, 'field_name': 'does_not_exist'})
request.user = self.superuser