[Django] #28826: AttributeError while trying to filter by annotion based on Q

15 views
Skip to first unread message

Django

unread,
Nov 21, 2017, 8:46:55 PM11/21/17
to django-...@googlegroups.com
#28826: AttributeError while trying to filter by annotion based on Q
-------------------------------------+-------------------------------------
Reporter: Ilya | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
{{{
class HumansManager(models.Manager):
def get_queryset(self):
wrapper = ExpressionWrapper(Q(payments__gt=F('salary')),
output_field=models.BooleanField())
return models.QuerySet(self.model).annotate(is_in_trouble=wrapper)


class Human(models.Model):
salary = models.IntegerField()
payments = models.IntegerField()
objects = HumansManager()
}}}

`is_in_trouble` works for
* `Human.objects.first().is_in_trouble`
* `Human.objects.values('is_in_trouble')`
But
`Human.objects.filter(is_in_trouble=True)` leads to
{{{
File "C:\spam\Python36-32\lib\site-packages\django\db\models\query.py",
line 246, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "C:\spam\Python36-32\lib\site-packages\django\db\models\query.py",
line 270, in __iter__
self._fetch_all()
File "C:\spam\Python36-32\lib\site-packages\django\db\models\query.py",
line 1178, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\spam\Python36-32\lib\site-packages\django\db\models\query.py",
line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch,
chunk_size=self.chunk_size)
File "C:\spam\Python36-32\lib\site-
packages\django\db\models\sql\compiler.py", line 1049, in execute_sql
sql, params = self.as_sql()
File "C:\spam\Python36-32\lib\site-
packages\django\db\models\sql\compiler.py", line 458, in as_sql
where, w_params = self.compile(self.where) if self.where is not None
else ("", [])
File "C:\spam\Python36-32\lib\site-
packages\django\db\models\sql\compiler.py", line 390, in compile
sql, params = node.as_sql(self, self.connection)
File "C:\spam\Python36-32\lib\site-
packages\django\db\models\sql\where.py", line 79, in as_sql
sql, params = compiler.compile(child)
File "C:\spam\Python36-32\lib\site-
packages\django\db\models\sql\compiler.py", line 390, in compile
sql, params = node.as_sql(self, self.connection)
File "C:\spam\Python36-32\lib\site-
packages\django\db\models\lookups.py", line 160, in as_sql
lhs_sql, params = self.process_lhs(compiler, connection)
File "C:\spam\Python36-32\lib\site-
packages\django\db\models\lookups.py", line 151, in process_lhs
lhs_sql, params = super().process_lhs(compiler, connection, lhs)
File "C:\spam\Python36-32\lib\site-
packages\django\db\models\lookups.py", line 77, in process_lhs
lhs = lhs.resolve_expression(compiler.query)
File "C:\spam\Python36-32\lib\site-
packages\django\db\models\expressions.py", line 248, in resolve_expression
for expr in c.get_source_expressions()
File "C:\spam\Python36-32\lib\site-
packages\django\db\models\expressions.py", line 248, in <listcomp>
for expr in c.get_source_expressions()
AttributeError: 'WhereNode' object has no attribute 'resolve_expression'
}}}

It really confusing.

--
Ticket URL: <https://code.djangoproject.com/ticket/28826>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 30, 2017, 8:28:11 AM11/30/17
to django-...@googlegroups.com
#28826: AttributeError while trying to filter by annotion based on Q
-------------------------------------+-------------------------------------
Reporter: Ilya | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #28863 which points out that this is a regression in Django
2.0.

--
Ticket URL: <https://code.djangoproject.com/ticket/28826#comment:1>

Reply all
Reply to author
Forward
0 new messages