Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#32649 closed Bug (fixed)

ModelAdmin.search_fields crashes for a search term with unbalanced quotes.

Reported by: Dlis Owned by: Mariusz Felisiak
Component: contrib.admin Version: 3.2
Severity: Release blocker Keywords: admin, search
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Dlis)

django.contrib.admin.ModelAdmin.search_fields now allows searching against quoted phrases with spaces but unfortunately search crashes for a search term like Foo "Bar "Baz" (with three quotes, some company names match such the pattern)

Change History (7)

comment:1 by Dlis, 3 years ago

Description: modified (diff)

comment:2 by Dlis, 3 years ago

The bug can be reproduced by the following way:

from django.utils.text import smart_split
from django.utils.text import unescape_string_literal

search_term = 'Foo "Bar "Baz"'
parts = list(smart_split(search_term))
unescape_string_literal(parts[1])

Result is ValueError: Not a string literal: '"Bar "Baz'

Last edited 3 years ago by Dlis (previous) (diff)

comment:3 by Dlis, 3 years ago

If somebody has such the problem, here is our temporary fix which was added to our internal basic ModelAdmin:

from django.utils import text


class ModelAdmin(...):

    ...

    def get_search_results(self, request, queryset, search_term):
        parts = search_term.split()
        try:
            for part in text.smart_split(search_term):
                text.unescape_string_literal(part)
        except ValueError:
            parts = map(lambda t: t.strip('"').strip("'"), parts)
        return super().get_search_results(request, queryset, ' '.join(parts))
Last edited 3 years ago by Dlis (previous) (diff)

comment:4 by Mariusz Felisiak, 3 years ago

Owner: changed from nobody to Mariusz Felisiak
Severity: NormalRelease blocker
Status: newassigned
Summary: django.contrib.admin.ModelAdmin.search_fields crashes for a search term with three quotesModelAdmin.search_fields crashes for a search term with unbalanced quotes.
Triage Stage: UnreviewedAccepted

Thanks for the report.

Regression in 26a413507abb38f7eee4cf62f2ee9727fdc7bf8d.
Reproduced at 9760e262f85ae57df39abe2799eff48a82b14474.

Last edited 3 years ago by Mariusz Felisiak (previous) (diff)

comment:5 by Mariusz Felisiak, 3 years ago

Has patch: set

comment:6 by GitHub <noreply@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 23fa29f6:

Fixed #32649 -- Fixed ModelAdmin.search_fields crash when searching against phrases with unbalanced quotes.

Thanks Dlis for the report.

Regression in 26a413507abb38f7eee4cf62f2ee9727fdc7bf8d.

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 59cce823:

[3.2.x] Fixed #32649 -- Fixed ModelAdmin.search_fields crash when searching against phrases with unbalanced quotes.

Thanks Dlis for the report.

Regression in 26a413507abb38f7eee4cf62f2ee9727fdc7bf8d.
Backport of 23fa29f6a6659e0f600d216de6bcb79e7f6818c9 from main

Note: See TracTickets for help on using tickets.
Back to Top