Re: [Django] #34402: Admin search_fields crashes for inherited model and __iexact lookup.

32 views
Skip to first unread message

Django

unread,
Mar 14, 2023, 10:34:36 AM3/14/23
to django-...@googlegroups.com
#34402: Admin search_fields crashes for inherited model and __iexact lookup.
--------------------------------+------------------------------------
Reporter: Pavel Pančocha | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------

Comment (by Pavel Pančocha):

Replying to [comment:1 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`?

--
Ticket URL: <https://code.djangoproject.com/ticket/34402#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 14, 2023, 10:55:07 AM3/14/23
to django-...@googlegroups.com
#34402: Admin search_fields crashes for inherited model and __iexact lookup.
--------------------------------+------------------------------------
Reporter: Pavel Pančocha | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------
Description changed by Pavel Pančocha:

Old description:

> 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
>
> Why? Because in `construct_search`
> ([[https://github.com/django/django/blob/4b1bfea2846f66f504265cec46ee1fe94ee9c98b/django/contrib/admin/options.py#L1123|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.

New description:

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`
([[https://github.com/django/django/blob/4b1bfea2846f66f504265cec46ee1fe94ee9c98b/django/contrib/admin/options.py#L1123|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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/34402#comment:5>

Django

unread,
Mar 18, 2023, 9:39:05 AM3/18/23
to django-...@googlegroups.com
#34402: Admin search_fields crashes for inherited model and __iexact lookup.
--------------------------------+------------------------------------
Reporter: Pavel Pančocha | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------

Comment (by Rahmat Faisal):

let me check

--
Ticket URL: <https://code.djangoproject.com/ticket/34402#comment:6>

Django

unread,
Mar 21, 2023, 10:01:49 AM3/21/23
to django-...@googlegroups.com
#34402: Admin search_fields crashes for inherited model and __iexact lookup.
--------------------------------+-----------------------------------------
Reporter: Pavel Pančocha | Owner: Rahmat Faisal
Type: Bug | Status: assigned

Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+-----------------------------------------
Changes (by Rahmat Faisal):

* owner: nobody => Rahmat Faisal
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34402#comment:7>

Django

unread,
Sep 17, 2023, 3:38:32 PM9/17/23
to django-...@googlegroups.com
#34402: Admin search_fields crashes for inherited model and __iexact lookup.
--------------------------------+-----------------------------------------
Reporter: Pavel Pančocha | Owner: Rahmat Faisal
Type: Bug | Status: assigned
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+-----------------------------------------

Comment (by 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 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)

--
Ticket URL: <https://code.djangoproject.com/ticket/34402#comment:8>

Django

unread,
Sep 17, 2023, 4:04:48 PM9/17/23
to django-...@googlegroups.com
#34402: Admin search_fields crashes for inherited model and __iexact lookup.
--------------------------------+-----------------------------------------
Reporter: Pavel Pančocha | Owner: Rahmat Faisal
Type: Bug | Status: assigned
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+-----------------------------------------
Changes (by Sarah Boyce):

* cc: Sarah Boyce (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/34402#comment:9>

Django

unread,
Nov 15, 2023, 9:43:15 AM11/15/23
to django-...@googlegroups.com
#34402: Admin search_fields crashes for inherited model and __iexact lookup.
--------------------------------+-----------------------------------------
Reporter: Pavel Pančocha | Owner: Rahmat Faisal
Type: Bug | Status: assigned
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+-----------------------------------------

Comment (by Pavel Pančocha):

Replying to [comment:8 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 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)


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.

--
Ticket URL: <https://code.djangoproject.com/ticket/34402#comment:10>

Django

unread,
Jan 15, 2025, 3:49:09 AM1/15/25
to django-...@googlegroups.com
#34402: Admin search_fields crashes for inherited model and __iexact lookup.
--------------------------------+------------------------------------
Reporter: Pavel Pančocha | Owner: Antoliny
Type: Bug | Status: assigned
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------
Changes (by Antoliny):

* owner: Rahmat Faisal => Antoliny

--
Ticket URL: <https://code.djangoproject.com/ticket/34402#comment:11>

Django

unread,
Jan 18, 2025, 5:32:23 AM1/18/25
to django-...@googlegroups.com
#34402: Admin search_fields crashes for inherited model and __iexact lookup.
--------------------------------+------------------------------------
Reporter: Pavel Pančocha | Owner: (none)
Type: Bug | Status: new
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------
Changes (by Antoliny):

* owner: Antoliny => (none)
* status: assigned => new

Comment:

I think the filter works as expected. 🧐
Since pk is recognized as a `OneToOneField`, it makes sense to refer to it
as `payer_ptr__pk__iexact`.
I might have misunderstood this issue.
--
Ticket URL: <https://code.djangoproject.com/ticket/34402#comment:12>

Django

unread,
Apr 20, 2025, 5:59:19 PM4/20/25
to django-...@googlegroups.com
#34402: Admin search_fields crashes for inherited model and __iexact lookup.
--------------------------------+----------------------------------------
Reporter: Pavel Pančocha | Owner: Ahmed Nassar
Type: Bug | Status: assigned
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+----------------------------------------
Changes (by Ahmed Nassar):

* owner: (none) => Ahmed Nassar
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/34402#comment:13>
Reply all
Reply to author
Forward
0 new messages