I have a query like: Foo.objects.extra(select={'extra_column':'SELECT ...'}), and I want to filter on the extra_column, but I cannot get this alias on filter. I am thinking of using annotate(), but I don't know annotate() can using on selectclause, all the tutorial is on SUM etc. I also tried HAVING clause, but it seems that Django doesn't support HAVING on extra().
Any suggestions will be very appreciated.
Edit:
Django 1.6, MySQL
I am query on some complicated condition, so I must using extra() to write the subquery. But I cannot using filter() to access the alias of the subquery. If I turned to annotate(), which can be accessed in later filter(), but seems that it only works with SUM, MAX, MIN, AVG, thus I cannot use it on my custom SELECT subquery.
I read the similar question here: Django ORM: Filter by extra attribute. It said that when query on a alias on MySQL, you need to use HAVING clause.
I don't know if this issue is also on other SQL backends like PostgreSQL, or on higher version of Django. Maybe there is no built-in solution on this question in Django1.6 with MySQL?