Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#15517 closed (fixed)

Admin search_field prefixes conflict with db.models.get_field_by_name

Reported by: fabianbuechler 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 fabianbuechler 5 years ago.
15517_search_fields_regression.diff (4.4 KB) - added by julien 5 years ago.

Download all attachments as: .zip

Change History (7)

Changed 5 years ago by fabianbuechler

comment:1 Changed 5 years ago by julien

  • Keywords blocker added
  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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

Changed 5 years ago by julien

comment:2 Changed 5 years ago by julien

  • 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 5 years ago by julien (previous) (diff)

comment:3 Changed 5 years ago by fabianbuechler

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

comment:4 Changed 5 years ago by ramiro

  • Resolution set to fixed
  • Status changed from new to closed

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 4 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

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