Opened 7 years ago
Closed 7 years ago
#28826 closed Bug (duplicate)
AttributeError while trying to filter by annotion based on Q
Reported by: | Ilya | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
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
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.
Note:
See TracTickets
for help on using tickets.
Duplicate of #28863 which points out that this is a regression in Django 2.0.