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 *,core_location.a + core_location.b as dist
FROM core_location,core_company
ORDER BY dist''')
The above code works as expected (the results are sorted by calculated field 'dist'), but when I add WHERE clause, for example:
Company.objects.raw('''SELECT *,core_location.a + core_location.b as dist
FROM core_location,core_company
WHERE dist<10
ORDER BY dist''')
i'm getting (1054, "Unknown column 'dist' in 'where clause'")
So far it looks like I cannot use calculated field in WHERE clause, but I can use it in ORDER BY statement. Please share your experience. Thank you.