外键关系的 GeoDjango 距离查询
我有以下模型(简化)
from django.contrib.gis.db import models 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()
我试图找到所有 modelB 对象并按距给定位置的距离对它们进行排序(其中距离定义为给定位置与关联 modelA 的点对象之间的距离)。当我尝试运行查询时
modelB.objects.distance((loc, field_name='modela__point')
,收到一条错误消息,
TypeError: ST_Distance output only available on GeometryFields.
指出 loc 是一个 Point 对象。但是,当我运行查询时,
modelB.objects.filter(modela__point__distance_lte = (loc, 1000))
此查询可以正常工作,不会出现错误,并且符合预期。
知道错误可能是什么吗?我正在使用 django 1.2.4、PostGis 1.5.2、PostGres 8.4。
谢谢。
I have the following models (simplified)
from django.contrib.gis.db import models 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第一个,您需要将其更改为:
如果您想查看所有 modelB 对象。
“.distance”用于计算距离字段并将其添加到 QuerySet(或 GeoQuerySet)的每个结果行。
For the first one, you will need to change it to:
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).