Opened 21 months ago
Last modified 12 months ago
#34402 assigned Bug
Admin search_fields crashes for inherited model and __iexact lookup. — at Version 5
Reported by: | Pavel Pančocha | Owned by: | nobody |
---|---|---|---|
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 (5)
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) |
---|
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
.