#36865: Query casting introduced in Django 5.2 makes queries miss DB indexes
------------------------------------+------------------------------------
Reporter: Mike Lissner | Owner: (none)
Type: Bug | Status: new
Component: contrib.admin | Version: 6.0
Severity: Normal | Resolution:
Keywords: search_fields cast | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
------------------------------------+------------------------------------
Changes (by Simon Charette):
* keywords: search, regression => search_fields cast
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
* stage: Unreviewed => Accepted
Comment:
[
https://github.com/django/django/pull/18765#pullrequestreview-2414452529
This problem was discussed in a follow up MR] to #26001.
As pointed out on
[
https://github.com/django/django/pull/20538/changes#r2694820830 the
proposed patch] (which you should prefix with #36865 to be automatically
linked here Mike) using either `field.to_python` (or
`field.formfield().to_python` as we're dealing with untrusted user input)
can likely be used as a signal to determine if a `cast` is necessary.
I'm also wondering why we even search against cast'ed to text integer
field when provided a string not composed of digits (AKA when it fails
`to_python`) when using the `exact` lookup. It seems like the logic could
be adjusted the other way around by avoiding exact lookups for search
terms that search fields fail `to_python` and we could drop the casting
altogether.
e.g.
{{{#!python
class Author(models.Model):
name = models.CharField()
dob = models.DateField()
class AuthorAdmin(models.ModelAdmin):
search_fields = ["name", "dob"]
}}}
then a search of the form `?q=foo 2010-01-15` should result in
{{{#!python
filter(
Q(name="foo")
| Q(name="2010-01-15")
| Q(dob=date(2010, 1, 15)
)
}}}
Where we don't even try `dob_str="foo"` as
`DateField.formfield().to_python("foo")` identity that the value is not
valid for this field and thus cannot be an exact match.
--
Ticket URL: <
https://code.djangoproject.com/ticket/36865#comment:1>