From 0bfaa55708d402432d44882d9a8e4cb85011472c Mon Sep 17 00:00:00 2001 From: Bendeguz Csirmaz Date: Wed, 11 Sep 2024 00:48:56 +0800 Subject: [PATCH] Fixed #35752 -- Fixed crash when using In() lookup in filters. --- django/db/models/lookups.py | 6 +++++- tests/lookup/tests.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py index 18c4f2ca08..734f911f83 100644 --- a/django/db/models/lookups.py +++ b/django/db/models/lookups.py @@ -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 diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py index 68adbe6496..df96546d04 100644 --- a/tests/lookup/tests.py +++ b/tests/lookup/tests.py @@ -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),