Opened 14 years ago

Closed 14 years ago

Last modified 13 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: no UI/UX: no

Description

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 14 years ago.
15517_search_fields_regression.diff (4.4 KB ) - added by Julien Phalip 14 years ago.

Download all attachments as: .zip

Change History (7)

by Fabian Büchler, 14 years ago

comment:1 by Julien Phalip, 14 years ago

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.

by Julien Phalip, 14 years ago

comment:2 by Julien Phalip, 14 years ago

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 14 years ago by Julien Phalip (previous) (diff)

comment:3 by Fabian Büchler, 14 years ago

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

comment:4 by Ramiro Morales, 14 years ago

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 by Jacob, 13 years ago

milestone: 1.3

Milestone 1.3 deleted

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