﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
14483	Implement subqueries in geodjango spatial lookup	milosu	nobody	"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.
"	New feature	closed	GIS	1.2	Normal	fixed	geodjango, subquery		Ready for checkin	1	0	0	0	0	0
