From 23fa29f6a6659e0f600d216de6bcb79e7f6818c9 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 14 Apr 2021 12:23:47 +0200 Subject: [PATCH] Fixed #32649 -- Fixed ModelAdmin.search_fields crash when searching against phrases with unbalanced quotes. Thanks Dlis for the report. Regression in 26a413507abb38f7eee4cf62f2ee9727fdc7bf8d. --- django/contrib/admin/options.py | 2 +- docs/releases/3.2.1.txt | 4 ++++ tests/admin_views/tests.py | 6 +++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 2324080ba1..1eaf4d8227 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -1025,7 +1025,7 @@ class ModelAdmin(BaseModelAdmin): orm_lookups = [construct_search(str(search_field)) for search_field in search_fields] for bit in smart_split(search_term): - if bit.startswith(('"', "'")): + if bit.startswith(('"', "'")) and bit[0] == bit[-1]: bit = unescape_string_literal(bit) or_queries = [models.Q(**{orm_lookup: bit}) for orm_lookup in orm_lookups] diff --git a/docs/releases/3.2.1.txt b/docs/releases/3.2.1.txt index cc716782e5..d5486f4cc9 100644 --- a/docs/releases/3.2.1.txt +++ b/docs/releases/3.2.1.txt @@ -26,3 +26,7 @@ Bugfixes * Fixed a bug in Django 3.2 where a system check would crash on a reverse one-to-one relationships in ``CheckConstraint.check`` or ``UniqueConstraint.condition`` (:ticket:`32635`). + +* Fixed a regression in Django 3.2 that caused a crash of + :attr:`.ModelAdmin.search_fields` when searching against phrases with + unbalanced quotes (:ticket:`32649`). diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index 69d03c3a95..4ec1c90349 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -3541,6 +3541,7 @@ class AdminSearchTest(TestCase): cls.per2 = Person.objects.create(name='Grace Hopper', gender=1, alive=False) cls.per3 = Person.objects.create(name='Guido van Rossum', gender=1, alive=True) Person.objects.create(name='John Doe', gender=1) + Person.objects.create(name='John O"Hara', gender=1) Person.objects.create(name="John O'Hara", gender=1) cls.t1 = Recommender.objects.create() @@ -3616,7 +3617,7 @@ class AdminSearchTest(TestCase): response = self.client.get(reverse('admin:admin_views_person_changelist') + '?q=Gui') self.assertContains( response, - """1 result (5 total)""", + """1 result (6 total)""", html=True ) @@ -3647,7 +3648,10 @@ class AdminSearchTest(TestCase): ("John Doe John", 0), ('"John Do"', 1), ("'John Do'", 1), + ("'John O\'Hara'", 0), ("'John O\\'Hara'", 1), + ('"John O\"Hara"', 0), + ('"John O\\"Hara"', 1), ] for search, hits in tests: with self.subTest(search=search):