使用点域过滤距离

问题描述 投票:0回答:1

我想通过使用点域在距离上设置滤镜

我有模特店

class Shop(models.Model):
     distance = models.PositiveIntegerField(null=True)
     Shop_location = models.PointField(null=True)

商店可以定义其ID显示下方的位置到另一个用户的距离。当用户共享其位置并且用户能够看到满足条件的商店时。条件是用户与商店之间计算的距离小于或等于商店给出的距离

django geodjango django-filter
1个回答
0
投票

我尚未测试,但我认为这可能有效您可以通过Distance对象和查找来完成,geodjango doc很好地说明了这一点。您还需要django F对象来获取每一行的距离值。我还假设您在商店模型中保存的距离为kilometerkm

from django.db.models import F
from django.contrib.gis.measure import D


user_location = Point() # your user location should be a standard `Point` object as well

# F object will get each shop's distance
# this query will get you all shops that their distance is less than or equal 
# of the distance between shop_location and user_location
in_distance_shops = Shop.objects.filter(shop_location__distance_lte=(user_location, D(km=F("distance"))))



© www.soinside.com 2019 - 2024. All rights reserved.