Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#15517 closed (fixed)

Admin search_field prefixes conflict with db.models.get_field_by_name

Reported by: Fabian Büchler Owned by: nobody
Component: contrib.admin Version: 1.3-beta
Severity: Keywords: blocker, search_fields, changelist
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


This issue might be related to #15203 and to the changeset r3601.

In the Django admin, the ModelAdmin.search_fields prefixes (^, =, @) are converted to __istartswith, __iexact and __search respectively in line 258 in django.contrib.admin.views.main:

    or_queries = [models.Q(**{construct_search(str(field_name)): bit}) for field_name in self.search_fields]

A few lines below in line 263 the field names are passed to django.db.models.options.Options.get_field_by_name without stripping the search prefixes first:

    f = self.lookup_opts.get_field_by_name(field_name)[0]

This results in FieldNotFound errors like "Event has no field named '=slug'".

I've attached a working patch.

Attachments (2)

patch_modeladmin_search_fields.diff (1.2 KB) - added by Fabian Büchler 7 years ago.
15517_search_fields_regression.diff (4.4 KB) - added by Julien Phalip 7 years ago.

Download all attachments as: .zip

Change History (7)

Changed 7 years ago by Fabian Büchler

comment:1 Changed 7 years ago by Julien Phalip

Keywords: blocker added
Needs tests: set
Triage Stage: UnreviewedAccepted

Thanks for the report. This is a regression introduced in [15526]. I'll post some tests shortly.

Changed 7 years ago by Julien Phalip

comment:2 Changed 7 years ago by Julien Phalip

Needs tests: unset

Your patch looks good. I've added tests for beginning ('^...') and exact ('=...') matches. Bizarrely I couldn't find any existing tests for these or for full-text matches ('@...') in the existing test suite.

Last edited 7 years ago by Julien Phalip (previous) (diff)

comment:3 Changed 7 years ago by Fabian Büchler

Awesome, thanks for the quick review and tests, Julien!

comment:4 Changed 7 years ago by Ramiro Morales

Resolution: fixed
Status: newclosed

In [15677]:

Fixed #15517 -- Fixed regression in admin search_fields option introduced in r15526. Thanks Fabian Buechler for the report and fix and Julien Phalip for adding tests.

comment:5 Changed 7 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

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