我的目的是根据网站OfferSite
获取所有优惠,并在OfferItem
模型上为今天的日期提供过滤。
当我在第二个模型上选择所有没有过滤器的时候,我得到了所需的输出(只有2个网站有offeritem_set),
In [23]: OfferSite.objects.all().select_related("offeritem")
Out[23]: [<OfferSite: OfferSite object>, <OfferSite: OfferSite object>]
但是当我尝试过滤第二个模型时,我得到了很多对象,我认为它会为每个OfferItem返回OfferSite
个对象。我期待只有两个OfferSite
个offeritem_set
个对象来获取所有已过滤的OfferItem
``
In [24]: OfferSite.objects.all().select_related("offeritem").filter(offeritem__offer_date=tod)
Out[24]: [<OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, <OfferSite: OfferSite object>, '...(remaining elements truncated)...']
是否有Django ORM方式获得所需的输出?
class OfferSite(models.Model):
name = models.CharField(max_length=30)
domain_url = models.URLField()
class OfferItem(models.Model):
title = models.CharField(max_length=255)
link = models.CharField(max_length=750)
image = ImageField()
price_after_discount = models.CharField(max_length=100, blank=True, null=True)
price_before_discount = models.CharField(max_length=100, blank=True, null=True)
discount = models.CharField(max_length=100, blank=True, null=True)
offer_date = models.DateField(auto_now_add=True, default=datetime.date.today())
offer_from = models.DateTimeField(blank=True, null=True)
offer_to = models.DateTimeField(blank=True, null=True)
single_item = models.BooleanField(default=True)
site = models.ForeignKey(OfferSite)
archived = models.BooleanField(default=False)
likes = models.IntegerField(max_length=4, default=0)
unlikes = models.IntegerField(max_length=4, default=0)
abusive = models.IntegerField(max_length=3, default=0)
答案 0 :(得分:4)
以防其他人正在寻找解决方案。这个问题的答案是Why does django's prefetch_related() only work with all() and not filter()?。这个问题的标题没有反映出这个问题所以我错过了它。
我的案例中的解决方案是,
OfferSite.objects.all().prefetch_related(Prefetch("offeritem_set", queryset=OfferItem.objects.filter(offer_date=tod), to_attr="offers"))
这仅适用于Django 1.7&amp;上方。