Opened 5 days ago
Last modified 4 days ago
#36787 assigned Bug
In and Range lookups crash when provided to filter() if RHS contains mix of expressions and strings — at Version 2
| Reported by: | Jacob Walls | Owned by: | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 5.2 |
| Severity: | Normal | Keywords: | |
| Cc: | Simon Charette, Clifford Gama, Varun Kasyap Pentamaraju | Triage Stage: | Accepted |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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.
-
tests/lookup/tests.py
diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py index 5b9dd8e5ec..a34d95b8cb 100644
a b class LookupQueryingTests(TestCase): 1855 1855 Season.objects.filter(In(F("year"), years)).order_by("pk"), seasons 1856 1856 ) 1857 1857 1858 def test_in_lookup_in_filter_text_field(self): 1859 self.assertSequenceEqual( 1860 # this works... 1861 # Season.objects.filter( 1862 # nulled_text_field__in=[F("nulled_text_field"), "special_value"] 1863 # ), 1864 # this doesn't... 1865 Season.objects.filter( 1866 In(F("nulled_text_field"), [F("nulled_text_field"), "special_value"]) 1867 ), 1868 [self.s2], 1869 ) 1870 1858 1871 def test_filter_lookup_lhs(self): 1859 1872 qs = Season.objects.annotate(before_20=LessThan(F("year"), 2000)).filter( 1860 1873 before_20=LessThan(F("year"), 1900),
====================================================================== 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).
Note:
See TracTickets
for help on using tickets.
Suggested patch is to just guard with
hasattr(self.lhs, "output_field").