我有一个类似的模型:
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是一个代表明天的值,今天它仍然有效,但是没有那条线。
我尝试过范围,但我失败了。任何提示?
答案 0 :(得分:0)
@Ignacio是对的。
featured = self.items.filter(date_from__lte = today,date_to__gte = today)
诀窍