首先:RHEL 6.5 上的 Python 2.7.6、Django 1.6.5、Postgres 9.3.4、PostGIS 2.1.3、psycopg2 2.5.3
这是相关模型:
class Location(models.Model):
name = models.CharField(max_length=255)
geometry = models.MultiPolygonField(blank=True, default=None, null=True)
objects = models.GeoManager() # override the default manager with a GeoManager instance
parent = models.ForeignKey('self', blank=True, default=None, null=True)
def __unicode__(self):
return self.name
这个查询应该有效根据文档:
touching_locations = Location.objects.filter(geometry__dwithin=(location.geometry, D(km=5)))
logging.debug(type(touching_locations))
logging.debug(len(touching_locations))
但事实并非如此。第一个调试调用有效,但第二个调用抛出ValueError
:
<class 'django.contrib.gis.db.models.query.GeoQuerySet'>
ValueError: Only numeric values of degree units are allowed on geographic DWithin queries.
如果我通过改变做一个小小的改变D(km=5)
to 5
:
touching_locations = Location.objects.filter(geometry__dwithin=(location.geometry, 5))
logging.debug(type(touching_locations))
logging.debug(len(touching_locations))
突然间它就起作用了。我得到的输出是这样的:
<class 'django.contrib.gis.db.models.query.GeoQuerySet'>
54
有谁知道为什么这不能按预期工作?这可能是一个错误,还是我犯了一个我没有看到的错误?
[edit]
我认为这可能是 Django 的一个错误。我继续开票here。一旦我弄清楚正确的解决方法是什么,我将在此处添加答案。