0

我有一个模型:

class FeaturedGroup(models.Model):
'''
Featured Group. Here we store the diferents groups of featureds items
'''
slug = models.SlugField(unique=True)
title = models.CharField(max_length=100)
description = models.TextField(blank=True)
picture = models.ImageField(upload_to="upload", default="img-tmp/featured_category.png")
items = models.ManyToManyField('FeaturedItem', blank=True, null=True)
cnt_show = models.IntegerField(_('Quantity of items'))
cnt_max = models.IntegerField(_('Max items'))
rotation_time = models.IntegerField()
price = CurrencyField(_("Price for each Freatured item"), decimal_places=2,
              max_digits=8, blank=True, null=True,
              help_text=_("Enter the price of the each featured item"))

我在那个类中有一个方法是:

def all_items(self, limit=False):
    key = 'featured_all_items_%(limit)s_%(id)s'%{'id':self.id, 'limit':limit}
    items = cache.get(key)
    if not items:
        featured = self.items.filter(date_from=datetime.date.today)
        featured = featured.order_by('-print_count', 'id')
        if limit:
            featured = featured[:self.cnt_show]
        items = []
        for item in featured:
            item.print_count += 1
            items.append(item.get_object())
            item.get_object()
            item.save()
        cache.set(key, items, settings.CACHE_TIME)
    return items

问题在于 self.items.filter(date_from=datetime.date.today) 的那条线,因为如果我有一个产品,例如 date_from 是一个代表昨天的值,但 date_to 是一个代表明天的值,今天它仍然是有效,但用那条线它不。

我已经尝试过范围,但我失败了。有什么提示吗?

4

1 回答 1

0

@Ignacio 是对的。

特色 = self.items.filter(date_from__lte=today, date_to__gte=today)

做到了

于 2013-11-29T08:35:19.423 回答