From 158eca4f93e492a6cc6f0240a0b6da3b7aa98ee6 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Mon, 26 Apr 2021 12:07:16 +0200 Subject: [PATCH] Refs #19080 -- Added tests for preserving select_related() in the admin changelist. --- tests/admin_changelist/admin.py | 1 + tests/admin_changelist/tests.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/tests/admin_changelist/admin.py b/tests/admin_changelist/admin.py index 4f23296eca..0397e8557a 100644 --- a/tests/admin_changelist/admin.py +++ b/tests/admin_changelist/admin.py @@ -33,6 +33,7 @@ site.register(Event, EventAdmin) class ParentAdmin(admin.ModelAdmin): list_filter = ['child__name'] search_fields = ['child__name'] + list_select_related = ['child'] class ChildAdmin(admin.ModelAdmin): diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py index 8da1c2f799..dc1b987a1e 100644 --- a/tests/admin_changelist/tests.py +++ b/tests/admin_changelist/tests.py @@ -104,6 +104,20 @@ class ChangeListTests(TestCase): cl = m.get_changelist_instance(request) self.assertEqual(cl.queryset.query.select_related, {'parent': {}}) + def test_select_related_preserved_when_multi_valued_in_search_fields(self): + parent = Parent.objects.create(name='Mary') + Child.objects.create(parent=parent, name='Danielle') + Child.objects.create(parent=parent, name='Daniel') + + m = ParentAdmin(Parent, custom_site) + request = self.factory.get('/parent/', data={SEARCH_VAR: 'daniel'}) + request.user = self.superuser + + cl = m.get_changelist_instance(request) + self.assertEqual(cl.queryset.count(), 1) + # select_related is preserved. + self.assertEqual(cl.queryset.query.select_related, {'child': {}}) + def test_select_related_as_tuple(self): ia = InvitationAdmin(Invitation, custom_site) request = self.factory.get('/invitation/')