gpt4 book ai didi

python - 如何使用 >= =< 更多过滤器获取查询

转载 作者:行者123 更新时间:2023-11-30 22:41:42 25 4
gpt4 key购买 nike

有代码

    shops = Shop.objects
shops.filter(id__in=list(set(shop_ids)))
if min:
shops.filter(delivery_price >= min)
if max:
shops.filter(delivery_price =< max)
shops.all()

但是delivery_price没有定义......我该如何修复它?并获取具有超过 1 个过滤器的查询集

class Shop(models.Model):
name = models.CharField(max_length=255, verbose_name=u'Название')
time_begin = models.TimeField(max_length=255,
verbose_name=u'Время начала работы')
time_end = models.TimeField(max_length=255,
verbose_name=u'Время окончания работы')
phone = models.CharField(max_length=255, verbose_name=u'Телефон')
preview = models.FileField(upload_to='files/shop/preview')
delivery_price = models.IntegerField(verbose_name=u'Стоимость доставки')

最佳答案

在 Django ORM 中,您没有使用比较符号,而是使用字段查找

shops = Shop.objects
shops.filter(id__in=list(set(shop_ids)))
if min:
shops.filter(delivery_price__gte=min)
if max:
shops.filter(delivery_price__lte=max)
shops.all()

Field lookups

关于保存查询集的@Sayse 评论

shops = Shop.objects.filter(id__in=list(set(shop_ids)))
if min:
shops = shops.filter(delivery_price__gte=min)
if max:
shops = shops.filter(delivery_price__lte=max)

另请注意,如果您使用默认管理器,则可以省略 .all()

关于python - 如何使用 >= =< 更多过滤器获取查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42420595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com