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.
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>
Comment (by Rahmat Faisal):
let me check
--
Ticket URL: <https://code.djangoproject.com/ticket/34402#comment:6>
* owner: nobody => Rahmat Faisal
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34402#comment:7>
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>
* cc: Sarah Boyce (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/34402#comment:9>
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>