diff options
| author | Bendeguz Csirmaz <csirmazbendeguz@gmail.com> | 2024-09-11 00:48:56 +0800 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-09-11 12:42:17 +0200 |
| commit | 0bfaa55708d402432d44882d9a8e4cb85011472c (patch) | |
| tree | f6d10d2de3b7dd9509bf55c68de4fee61e44d2b4 | |
| parent | 727587c08955e4e42a5b82bfb75d51517b50c976 (diff) | |
Fixed #35752 -- Fixed crash when using In() lookup in filters.
| -rw-r--r-- | django/db/models/lookups.py | 6 | ||||
| -rw-r--r-- | tests/lookup/tests.py | 20 |
2 files changed, 25 insertions, 1 deletions
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), |
