无法将关键字'distance_gte'解析为字段。选项包括:Shop_category,地址,距离,id,名称,shop_location,用户,user_id

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

我有两个样板店和客户

商店

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

python django django-haystack geodjango django-filters
1个回答
0
投票

您需要使用双下划线进行查找,所以:

from django.contrib.gis.db.models.functions import Distance

Shop_list = Shop.objects.filter(
    distance__gte=Distance(
        'shop_location',    
        request.user.customer.customer_location
    )
)
© www.soinside.com 2019 - 2024. All rights reserved.