Opened 39 minutes ago

Last modified 15 minutes ago

#36787 new Bug

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

Reported by: Jacob Walls Owned by:
Component: Database layer (models, ORM) Version: 5.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

  • tests/lookup/tests.py

    diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py
    index 5b9dd8e5ec..4cb90ed1d9 100644
    a b class LookupQueryingTests(TestCase):  
    18471847            ((1842, 2042), (self.s2, self.s3)),
    18481848            ((1942, 1942, 1942), (self.s1,)),
    18491849            ((1942, 2042, 1842), (self.s1, self.s2, self.s3)),
     1850            # Mix expressions and string field references.
     1851            ((models.F("year") + 100, "gt"), (self.s1,)),
    18501852        ]
    18511853
    18521854        for years, seasons in test_cases:
======================================================================
ERROR: test_in_lookup_in_filter (lookup.tests.LookupQueryingTests.test_in_lookup_in_filter) (years=(<CombinedExpression: F(year) + Value(100)>, 'gt'), seasons=(<Season: 1942>,))
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/jwalls/django/tests/lookup/tests.py", line 1857, in test_in_lookup_in_filter
    Season.objects.filter(In(F("year"), years)).order_by("pk"), seasons
                          ~~^^^^^^^^^^^^^^^^^^
  File "/Users/jwalls/django/django/db/models/lookups.py", line 35, in __init__
    self.rhs = self.get_prep_lookup()
               ~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/jwalls/django/django/db/models/lookups.py", line 517, in get_prep_lookup
    return super().get_prep_lookup()
           ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/jwalls/django/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 (1)

comment:1 by Jacob Walls, 15 minutes ago

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

Last edited 15 minutes ago by Jacob Walls (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top