[Django] #36787: In and Range lookups crash when provided to filter() if RHS contains mix of expressions and strings

12 views
Skip to first unread message

Django

unread,
Dec 9, 2025, 3:05:21 PM (13 days ago) Dec 9
to django-...@googlegroups.com
#36787: In and Range lookups crash when provided to filter() if RHS contains mix of
expressions and strings
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
{{{#!diff
diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py
index 5b9dd8e5ec..4cb90ed1d9 100644
--- a/tests/lookup/tests.py
+++ b/tests/lookup/tests.py
@@ -1847,6 +1847,8 @@ class LookupQueryingTests(TestCase):
((1842, 2042), (self.s2, self.s3)),
((1942, 1942, 1942), (self.s1,)),
((1942, 2042, 1842), (self.s1, self.s2, self.s3)),
+ # Mix expressions and string field references.
+ ((models.F("year") + 100, "gt"), (self.s1,)),
]

for years, seasons in test_cases:
).exists()
}}}
{{{#!py
======================================================================
ERROR: test_in_lookup_in_filter
(lookup.tests.LookupQueryingTests.test_in_lookup_in_filter)
(years=(<CombinedExpression: F(year) + Value(100)>, 'gt'),
seasons=(<Season: 1942>,))
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/jwalls/django/tests/lookup/tests.py", line 1857, in
test_in_lookup_in_filter
Season.objects.filter(In(F("year"), years)).order_by("pk"), seasons
~~^^^^^^^^^^^^^^^^^^
File "/Users/jwalls/django/django/db/models/lookups.py", line 35, in
__init__
self.rhs = self.get_prep_lookup()
~~~~~~~~~~~~~~~~~~~~^^
File "/Users/jwalls/django/django/db/models/lookups.py", line 517, in
get_prep_lookup
return super().get_prep_lookup()
~~~~~~~~~~~~~~~~~~~~~~~^^
File "/Users/jwalls/django/django/db/models/lookups.py", line 310, in
get_prep_lookup
Value(prep_value, self.lhs.output_field)
^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'F' object has no attribute 'output_field'
}}}
Seems like 089deb82b9ac2d002af36fd36f288368cdac4b53 (5.2) needed the same
tweak as 0bfaa55708d402432d44882d9a8e4cb85011472c (5.2).
--
Ticket URL: <https://code.djangoproject.com/ticket/36787>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 9, 2025, 3:28:34 PM (13 days ago) Dec 9
to django-...@googlegroups.com
#36787: In and Range lookups crash when provided to filter() if RHS contains mix of
expressions and strings
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jacob Walls):

Suggested patch is to just guard with `hasattr("output_field")`.
--
Ticket URL: <https://code.djangoproject.com/ticket/36787#comment:1>

Django

unread,
Dec 9, 2025, 4:12:42 PM (13 days ago) Dec 9
to django-...@googlegroups.com
#36787: In and Range lookups crash when provided to filter() if RHS contains mix of
expressions and strings
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Jacob Walls:

Old description:
New description:

The invariance between `__in` and `In` is broken when the right hand side
contains a mix of expressions and strings (where the strings are possible
literal values). EDIT: replaced draft test case, first version was
misleading.
{{{#!diff
diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py
index 5b9dd8e5ec..a34d95b8cb 100644
--- a/tests/lookup/tests.py
+++ b/tests/lookup/tests.py
@@ -1855,6 +1855,19 @@ class LookupQueryingTests(TestCase):
Season.objects.filter(In(F("year"),
years)).order_by("pk"), seasons
)

+ def test_in_lookup_in_filter_text_field(self):
+ self.assertSequenceEqual(
+ # this works...
+ # Season.objects.filter(
+ # nulled_text_field__in=[F("nulled_text_field"),
"special_value"]
+ # ),
+ # this doesn't...
+ Season.objects.filter(
+ In(F("nulled_text_field"), [F("nulled_text_field"),
"special_value"])
+ ),
+ [self.s2],
+ )
+
def test_filter_lookup_lhs(self):
qs = Season.objects.annotate(before_20=LessThan(F("year"),
2000)).filter(
before_20=LessThan(F("year"), 1900),

}}}
{{{#!py
======================================================================
ERROR: test_in_lookup_in_filter_text_field
(lookup.tests.LookupQueryingTests.test_in_lookup_in_filter_text_field)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/django/source/tests/lookup/tests.py", line 1866, in
test_in_lookup_in_filter_text_field
In(F("nulled_text_field"), [F("nulled_text_field"), "special_value"])
File "/django/source/django/db/models/lookups.py", line 35, in __init__
self.rhs = self.get_prep_lookup()
^^^^^^^^^^^^^^^^^^^^^^
File "/django/source/django/db/models/lookups.py", line 517, in
get_prep_lookup
return super().get_prep_lookup()
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/django/source/django/db/models/lookups.py", line 310, in
get_prep_lookup
Value(prep_value, self.lhs.output_field)
^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'F' object has no attribute 'output_field'
}}}
Seems like 089deb82b9ac2d002af36fd36f288368cdac4b53 (5.2) needed the same
tweak as 0bfaa55708d402432d44882d9a8e4cb85011472c (5.2).

--
--
Ticket URL: <https://code.djangoproject.com/ticket/36787#comment:2>

Django

unread,
Dec 9, 2025, 8:15:16 PM (13 days ago) Dec 9
to django-...@googlegroups.com
#36787: In and Range lookups crash when provided to filter() if RHS contains mix of
expressions and strings
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
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 Natalia Bidart):

* cc: Simon Charette (added)
* stage: Unreviewed => Accepted

Comment:

Thank you for the edits, now it makes sense!
--
Ticket URL: <https://code.djangoproject.com/ticket/36787#comment:3>

Django

unread,
Dec 10, 2025, 1:09:26 AM (12 days ago) Dec 10
to django-...@googlegroups.com
#36787: In and Range lookups crash when provided to filter() if RHS contains mix of
expressions and strings
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
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 Clifford Gama):

* cc: Clifford Gama (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/36787#comment:4>

Django

unread,
Dec 10, 2025, 4:38:06 AM (12 days ago) Dec 10
to django-...@googlegroups.com
#36787: In and Range lookups crash when provided to filter() if RHS contains mix of
expressions and strings
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Pravin
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
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 Pravin):

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

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

Django

unread,
Dec 10, 2025, 4:39:13 AM (12 days ago) Dec 10
to django-...@googlegroups.com
#36787: In and Range lookups crash when provided to filter() if RHS contains mix of
expressions and strings
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
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 Pravin):

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

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

Django

unread,
Dec 10, 2025, 4:39:45 AM (12 days ago) Dec 10
to django-...@googlegroups.com
#36787: In and Range lookups crash when provided to filter() if RHS contains mix of
expressions and strings
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
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 Pravin):

sorry I should have asked before assigned to me.
--
Ticket URL: <https://code.djangoproject.com/ticket/36787#comment:7>

Django

unread,
Dec 10, 2025, 10:33:17 AM (12 days ago) Dec 10
to django-...@googlegroups.com
#36787: In and Range lookups crash when provided to filter() if RHS contains mix of
expressions and strings
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
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 JaeHyuckSa):

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

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

Django

unread,
Dec 11, 2025, 5:47:29 AM (11 days ago) Dec 11
to django-...@googlegroups.com
#36787: In and Range lookups crash when provided to filter() if RHS contains mix of
expressions and strings
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
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 Varun Kasyap Pentamaraju):

* cc: Varun Kasyap Pentamaraju (added)

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

Django

unread,
Dec 21, 2025, 8:39:09 AM (yesterday) Dec 21
to django-...@googlegroups.com
#36787: In and Range lookups crash when provided to filter() if RHS contains mix of
expressions and strings
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by JaeHyuckSa):

* has_patch: 0 => 1

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

Django

unread,
9:35 AM (2 hours ago) 9:35 AM
to django-...@googlegroups.com
#36787: In and Range lookups crash when provided to filter() if RHS contains mix of
expressions and strings
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* stage: Accepted => Ready for checkin

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