#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)
Change History (7)
by , 14 years ago
Attachment: | patch_modeladmin_search_fields.diff added |
---|
comment:1 by , 14 years ago
Keywords: | blocker added |
---|---|
Needs tests: | set |
Triage Stage: | Unreviewed → Accepted |
by , 14 years ago
Attachment: | 15517_search_fields_regression.diff added |
---|
comment:2 by , 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.
Thanks for the report. This is a regression introduced in [15526]. I'll post some tests shortly.