Opened 38 hours ago

Last modified 18 hours ago

#36787 assigned Bug

In and Range lookups crash when provided to filter() if RHS contains mix of expressions and strings

Reported by: Jacob Walls Owned by: JaeHyuckSa
Component: Database layer (models, ORM) Version: 5.2
Severity: Normal Keywords:
Cc: Simon Charette, Clifford Gama 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 Jacob Walls)

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):  
    18551855                    Season.objects.filter(In(F("year"), years)).order_by("pk"), seasons
    18561856                )
    18571857
     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
    18581871    def test_filter_lookup_lhs(self):
    18591872        qs = Season.objects.annotate(before_20=LessThan(F("year"), 2000)).filter(
    18601873            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).

Change History (8)

comment:1 by Jacob Walls, 37 hours ago

Suggested patch is to just guard with hasattr("output_field").

Version 0, edited 37 hours ago by Jacob Walls (next)

comment:2 by Jacob Walls, 37 hours ago

Description: modified (diff)

comment:3 by Natalia Bidart, 33 hours ago

Cc: Simon Charette added
Triage Stage: UnreviewedAccepted

Thank you for the edits, now it makes sense!

comment:4 by Clifford Gama, 28 hours ago

Cc: Clifford Gama added

comment:5 by Pravin, 24 hours ago

Owner: set to Pravin
Status: newassigned

comment:6 by Pravin, 24 hours ago

Owner: Pravin removed
Status: assignednew

comment:7 by Pravin, 24 hours ago

sorry I should have asked before assigned to me.

comment:8 by JaeHyuckSa, 18 hours ago

Owner: set to JaeHyuckSa
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top