#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/056.2396f47f4435e73f7a0c2f334f617c83%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to