相关模型的GeoDjango距离
我试图从距离相关模型的距离返回一个查询集。相关模型的GeoDjango距离
models.py(简体)
class Store(models.Model):     geopoint = models.PointField(srid=4326) 
    objects = models.GeoManager() 
class HashTag(models.Model): 
    tag = models.CharField(max_length=100) 
class Label(models.Model): 
    hashtags = models.ManyToManyField(HashTag) 
    store = models.ForeignKey(Store) 
我需要返回是具有从给定的点按距离排序一定的标记/标签的标签对象。
Label.objects.filter(hashtags__in=tags) 距离可在存储对象与计算:
Store.objects.filter(label__hashtags__in=tags)       .distance(location).order_by('distance') 
我想要做的就是在Label表来执行查询
标签可以通过找到返回一切,但我怀疑这是不可能的。
试图在查询集结果distance方法:
TypeError: ST_Distance output only available on GeometryFields. 失败,这将是有意义的做最有效的退而求其次。我能想出的唯一解决方案是执行两个查询并将结果合并到一个集合中。
回答:
这是绝对有可能实现您的查询:
- 使用annotate()每Label对象上追加的距离值。
- 使用Distance()函数来计算每家商店的geopoint和location点
查询应该是这样的距离:
from django.contrib.gis.db.models.functions import Distance Label.objects.filter(hashtags__in=tags) 
      .annotate(distance=Distance('store__geopoint', location)) 
      .order_by('distance') 
以上是 相关模型的GeoDjango距离 的全部内容, 来源链接: utcz.com/qa/267201.html








