简体   繁体   中英

how get query with >= =< more filters

Have code

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

but delivery_price is not defined.... how i can fix it? and get queryset with more than 1 filter

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

In Django ORM you are not using comparison signs you are using field lookups

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

per @Sayse comment regarding your saving of queryset

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)

Also note out if you are using default manager you can ommit .all()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM