I\'m wondering if there are any limitations on syntax of raw() method when using calculated fields. Here is a quick example:
Company.objects.raw(\'\'\'SELECT
You can use the HAVING
clause for derived columns. BTW - this includes columns which are aggregations e.g. the result of SUM, COUNT etc.
So, the following should work:
Company.objects.raw('''SELECT *,core_location.a + core_location.b as dist
FROM core_location,core_company
HAVING dist<10
ORDER BY dist''')