Opened 20 months ago
Last modified 12 months ago
#34402 assigned Bug
Admin search_fields crashes for inherited model and __iexact lookup.
Reported by: | Pavel Pančocha | Owned by: | Rahmat Faisal |
---|---|---|---|
Component: | contrib.admin | Version: | 3.2 |
Severity: | Normal | Keywords: | |
Cc: | Sarah Boyce | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Hello everyone!
Let's have model Payer
and we will create ExtendedPayer
from it
class ExtendedPayer(Payer): ...
We create admin for ExtendedPayer
and define search fields. We want to be able to filter by pk
of Payer
. So I would expect these to work the same:
class ExtendedPayerAdmin(ModelAdmin): ... search_fields = ( "pk__iexact", ... ) ...
class ExtendedPayerAdmin(ModelAdmin): ... search_fields = ( "id__iexact", ... ) ...
Guess what? The "id" variant works ok and the "pk" fails with Related Field got invalid lookup: iexact
Why? Because in construct_search
(See Git) it's transformed to pk__iexact__icontains
as it found out that the pk
is payer_ptr
which is FK.
If we want to be correct, it should be "payer_ptr__pk__iexact"
.
Please let me know if this behaviour is a bug or if is there any reason behind it.
Change History (10)
comment:2 by , 20 months ago
Triage Stage: | Unreviewed → Accepted |
---|
Agreed, this restriction should be documented or fixed.
comment:3 by , 20 months ago
Summary: | Admin searchfields for inherited model → Admin search_fields crashes for inherited model and __iexact lookup. |
---|
comment:4 by , 20 months ago
Replying to Mariusz Felisiak:
All cases
pk
,payer__ptr
, andid
works with the__exact
lookup. Do you need__iexact
?
The main difference is that
id
isAutoField
,pk
andpayer_ptr
are recognized asOneToOneField
.
Yes, because the exact
fails in the search in the admin. When I try to search something else with chars (eg. test
) and I have the lookup for pk__exact
if it fails with the exception it cannot convert the test
to int. It works for iexact
- maybe it is a problem already fixed in newer Django version.
What is the proposed solution here? Should the resulting query be transformed to payer_ptr__pk__iexact
?
comment:5 by , 20 months ago
Description: | modified (diff) |
---|
comment:7 by , 20 months ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
follow-up: 10 comment:8 by , 14 months ago
Can you not define the search_fields here to be payer_ptr__pk__iexact
(as you would when constructing a filter)?
I feel like transforming the query for the search_fields
might be surprising as this error is consistent as to if you were to do this with .filter
.
(If we want this transforming behaviour I have a patch ready, just not convinced it's what we want)
comment:9 by , 14 months ago
Cc: | added |
---|
comment:10 by , 12 months ago
Replying to Sarah Boyce:
Can you not define the search_fields here to be
payer_ptr__pk__iexact
(as you would when constructing a filter)?
I feel like transforming the query for thesearch_fields
might be surprising as this error is consistent as to if you were to do this with.filter
.
(If we want this transforming behaviour I have a patch ready, just not convinced it's what we want)
Sorry, I don't get your comment. The search fields are ("pk__iexact",...
. Or what else do you propose? The issue is, that it differs in the way it works with "id" and how with "pk".
If I can help more, let me know please.
All cases
pk
,payer__ptr
, andid
works with the__exact
lookup. Do you need__iexact
?The main difference is that
id
isAutoField
,pk
andpayer_ptr
are recognized asOneToOneField
.