How can I annotate a Django QuerySet with data from a custom join expression, without using raw SQL?
I'd like to translate the following query for the Django ORM, without having to use this question :
SELECT a.*, b.name as b_name
FROM a
JOIN b ON ST_Within(ST_Centroid(a.geom), b.geom)
As far as I could tell, the best candidate for doing something like this it the annotate(...)
function, but the documentation didn't have anything on how to add a joined table to the annotated QuerySet.
My other idea was to use something similar to ManyToManyField
(maybe subclass it) that can use custom ON ...
expressions for its joined model.
Any other idea?