Opened 9 years ago

Closed 9 years ago

#24764 closed Bug (wontfix)

Using 'startswith' field lookup with F objects causes AttributeError

Reported by: Petros Moisiadis Owned by: nobody
Component: Database layer (models, ORM) Version: 1.7
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

In Django 1.7.8, when using "startswith" field lookup with F objects in the same query, the following AttributeError exception is raised:

>>> objects = MyObject.objects.filter(name__startswith=F('description'))
>>> objects
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 116, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 141, in __iter__
    self._fetch_all()
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 966, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 265, in iterator
    for row in compiler.results_iter():
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 701, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 776, in execute_sql
    sql, params = self.as_sql()
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 110, in as_sql
    where, w_params = self.compile(self.query.where)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 81, in compile
    return node.as_sql(self, self.connection)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/sql/where.py", line 106, in as_sql
    sql, params = qn.compile(child)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 81, in compile
    return node.as_sql(self, self.connection)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/lookups.py", line 152, in as_sql
    rhs_sql = self.get_rhs_op(connection, rhs_sql)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/lookups.py", line 258, in get_rhs_op
    return connection.pattern_ops[self.lookup_name] % rhs
AttributeError: 'DatabaseWrapper' object has no attribute 'pattern_ops'

The following (without the 'startswith' field lookup), however, works:

>>> objects = MyObject.objects.filter(name=F('description'))
>>> objects
[...]

Interestingly, Django 1.8 does not have this bug.

Change History (2)

comment:1 by Petros Moisiadis, 9 years ago

Strangely, the following (using 'endswith' instead of 'startswith' field lookup) also works:

>>> objects = MyObject.objects.filter(name__endswith=F('description'))
>>> objects
[...]

comment:2 by Tim Graham, 9 years ago

Resolution: wontfix
Status: newclosed

This was likely fixed in the query expressions work in Django 1.8. Since 1.7 is only receiving security and data loss fixes, we won't fix this issue there.

Note: See TracTickets for help on using tickets.
Back to Top