I’m trying to create a Django query that will do the equivalent of the following PostgreSQL/PostGIS query:
SELECT DISTINCT ON (site) * FROM some_table;
site is a POINT type geometry column. How can this be done?
Basically, many of the records in some_table share the same POINT geometry; I just want a list of the geometries with no duplicates. I don’t care about the rest of the some_table columns.
The rest of my query is pretty simple; it looks something like this:
qs = models.SomeTable.objects.filter(foo='bar', site__contained=some_polygon)
Side note:
The ‘manager’ for SomeTable (SomeTable.objects) is a django.contrib.gis.db.models.GeoManger type. I don’t know if that helps at all.
Relevant version info:
- Django 1.3
- PostgreSQL 9.1.1
- PostGIS 1.5.3
I figured it out. I had overlooked
distinct: https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.distinctHere’s the django query that does exactly what I need: