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 Pavel Pančocha)

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:1 by Mariusz Felisiak, 20 months ago

All cases pk, payer__ptr, and id works with the __exact lookup. Do you need __iexact?

The main difference is that id is AutoField, pk and payer_ptr are recognized as OneToOneField.

Last edited 20 months ago by Mariusz Felisiak (previous) (diff)

comment:2 by Mariusz Felisiak, 20 months ago

Triage Stage: UnreviewedAccepted

Agreed, this restriction should be documented or fixed.

Last edited 20 months ago by Tim Graham (previous) (diff)

comment:3 by Mariusz Felisiak, 20 months ago

Summary: Admin searchfields for inherited modelAdmin search_fields crashes for inherited model and __iexact lookup.

in reply to:  1 comment:4 by Pavel Pančocha, 20 months ago

Replying to Mariusz Felisiak:

All cases pk, payer__ptr, and id works with the __exact lookup. Do you need __iexact?

The main difference is that id is AutoField, pk and payer_ptr are recognized as OneToOneField.

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 Pavel Pančocha, 20 months ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top