Fixed #35752 -- Fixed crash when using In() lookup in filters.

This commit is contained in:
Bendeguz Csirmaz 2024-09-11 00:48:56 +08:00 committed by Sarah Boyce
parent 727587c089
commit 0bfaa55708
2 changed files with 25 additions and 1 deletions

View File

@ -300,7 +300,11 @@ class FieldGetDbPrepValueIterableMixin(FieldGetDbPrepValueMixin):
# An expression will be handled by the database but can coexist
# alongside real values.
pass
elif self.prepare_rhs and hasattr(self.lhs.output_field, "get_prep_value"):
elif (
self.prepare_rhs
and hasattr(self.lhs, "output_field")
and hasattr(self.lhs.output_field, "get_prep_value")
):
rhs_value = self.lhs.output_field.get_prep_value(rhs_value)
prepared_values.append(rhs_value)
return prepared_values

View File

@ -24,6 +24,7 @@ from django.db.models.lookups import (
Exact,
GreaterThan,
GreaterThanOrEqual,
In,
IsNull,
LessThan,
LessThanOrEqual,
@ -1511,6 +1512,25 @@ class LookupQueryingTests(TestCase):
[self.s1, self.s3],
)
def test_in_lookup_in_filter(self):
test_cases = [
((), ()),
((1942,), (self.s1,)),
((1842,), (self.s2,)),
((2042,), (self.s3,)),
((1942, 1842), (self.s1, self.s2)),
((1942, 2042), (self.s1, self.s3)),
((1842, 2042), (self.s2, self.s3)),
((1942, 1942, 1942), (self.s1,)),
((1942, 2042, 1842), (self.s1, self.s2, self.s3)),
]
for years, seasons in test_cases:
with self.subTest(years=years, seasons=seasons):
self.assertSequenceEqual(
Season.objects.filter(In(F("year"), years)).order_by("pk"), seasons
)
def test_filter_lookup_lhs(self):
qs = Season.objects.annotate(before_20=LessThan(F("year"), 2000)).filter(
before_20=LessThan(F("year"), 1900),