mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #26001 -- Fixed non-string field exact lookups in ModelAdmin.search_fields.
This commit is contained in:
@@ -48,6 +48,7 @@ class ChildAdmin(admin.ModelAdmin):
|
||||
list_display = ["name", "parent"]
|
||||
list_per_page = 10
|
||||
list_filter = ["parent", "age"]
|
||||
search_fields = ["age__exact", "name__exact"]
|
||||
|
||||
def get_queryset(self, request):
|
||||
return super().get_queryset(request).select_related("parent")
|
||||
|
||||
@@ -860,6 +860,25 @@ class ChangeListTests(TestCase):
|
||||
cl = m.get_changelist_instance(request)
|
||||
self.assertCountEqual(cl.queryset, [abcd])
|
||||
|
||||
def test_search_with_exact_lookup_for_non_string_field(self):
|
||||
child = Child.objects.create(name="Asher", age=11)
|
||||
model_admin = ChildAdmin(Child, custom_site)
|
||||
|
||||
for search_term, expected_result in [
|
||||
("11", [child]),
|
||||
("Asher", [child]),
|
||||
("1", []),
|
||||
("A", []),
|
||||
("random", []),
|
||||
]:
|
||||
request = self.factory.get("/", data={SEARCH_VAR: search_term})
|
||||
request.user = self.superuser
|
||||
with self.subTest(search_term=search_term):
|
||||
# 1 query for filtered result, 1 for filtered count, 1 for total count.
|
||||
with self.assertNumQueries(3):
|
||||
cl = model_admin.get_changelist_instance(request)
|
||||
self.assertCountEqual(cl.queryset, expected_result)
|
||||
|
||||
def test_no_distinct_for_m2m_in_list_filter_without_params(self):
|
||||
"""
|
||||
If a ManyToManyField is in list_filter but isn't in any lookup params,
|
||||
|
||||
Reference in New Issue
Block a user