我有两个样板店和客户
class Shop(models.Model):
address = models.CharField(max_length=70, null=True)
Shop_category = models.CharField(max_length=200, null=True, )
Name=models.CharField(max_length=70)
distance = models.PositiveIntegerField(null=True)
shop_location = models.PointField(null=True, default=Point(28.49, 77.33))
class Customer(models.Model):
user = models.OneToOneField(User, null=True,
on_delete=models.CASCADE)
customer_location = models.PointField(null=True, default=Point(28.49, 77.33))
我想在商店设置过滤器。如果商店与顾客之间的距离小于商店给出的距离,商店将向顾客显示。和我的views.py
from django.contrib.gis.db.models.functions import Distance
Shop_list = Shop.objects.filter(
distance_gte=Distance('shop_location',
request.user.customer.customer_location)
)
我遇到错误,无法将关键字'distance_gte'解析为字段。选择是:商店类别,地址,距离,ID,名称,商店位置,用户,用户ID
您需要使用双下划线进行查找,所以:
from django.contrib.gis.db.models.functions import Distance
Shop_list = Shop.objects.filter(
distance__gte=Distance(
'shop_location',
request.user.customer.customer_location
)
)