GeoDjango distance query for a ForeignKey Relationship
I have the following models (simplified)
from django.contrib.gis.db import m开发者_JAVA技巧odels as geomodels
modelB (geomodels.Model):
objects = geomodels.GeoManager()
modelA (geomodels.Model):
point = geomodels.PointField(unique=True)
mb = models.ForeignKey(modelB,related_name='modela')
objects = geomodels.GeoManager()
I am trying to find all modelB objects and sort them by distance from a given location (where distance is defined as distance between a given location and the point object of associated modelA). When I try to run the query
modelB.objects.distance((loc, field_name='modela__point')
I get an error saying
TypeError: ST_Distance output only available on GeometryFields.
Note that loc is a Point object.However, when I run the query
modelB.objects.filter(modela__point__distance_lte = (loc, 1000))
this query works without error and as expected.
Any idea what the mistake could be? I am using django 1.2.4, PostGis 1.5.2, PostGres 8.4.
Thanks.
For the first one, you will need to change it to:
modelB.objects.all().distance(loc, field_name='modela__point')
if you want to see all modelB objects.
The ".distance" is used to calculate and add a distance field to each resulting row of the QuerySet (or GeoQuerySet).
精彩评论