Implement subqueries in geodjango spatial lookup
|Reported by:||milosu||Owned by:||nobody|
|Cc:||Triage Stage:||Ready for checkin|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Currently only GEOS geometry objects can be used in the GeoQuerySet spatial field lookups. Sometimes the geometry objects are very large geometries on its own and the resulting SQL statement that includes EWKB of the geometry is very large (lets say 30 mega bytes in my case).
Attached patch extends GeoDjango ORM to support subqueries in spatial lookups, so that it is possible to make a query like:
qset = Address.objects.filter(location__within = District.objects.filter(name='Boston area').values('area'))
so that the resulting spatial SQL looks like:
SELECT "spatial_subquery_address"."id", "spatial_subquery_address"."city", "spatial_subquery_address"."location" FROM "spatial_subquery_address" WHERE ST_Within("spatial_subquery_address"."location", (SELECT U0."area" FROM "spatial_subquery_district" U0 WHERE U0."name" = Boston area ))
See attached regression test for this enhancement.
Patch was built against slightly modified Django 1.2.3 so that it will not apply cleanly, but I hope jbronn or someone else should get the idea from my attached version.