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

时间:2017-02-23 15:59:42

标签: python django

有代码

    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没有定义......我怎么能解决它? 并获取带有多个过滤器的查询集

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'Стоимость доставки')

1 个答案:

答案 0 :(得分:3)

在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()