我想通过使用点域在距离上设置滤镜
我有模特店
class Shop(models.Model):
distance = models.PositiveIntegerField(null=True)
Shop_location = models.PointField(null=True)
商店可以定义其ID显示下方的位置到另一个用户的距离。当用户共享其位置并且用户能够看到满足条件的商店时。条件是用户与商店之间计算的距离小于或等于商店给出的距离
我尚未测试,但我认为这可能有效您可以通过Distance
对象和查找来完成,geodjango doc很好地说明了这一点。您还需要django F
对象来获取每一行的距离值。我还假设您在商店模型中保存的距离为kilometer
或km
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"))))