【发布时间】:2012-04-05 17:33:38
【问题描述】:
class Review(models.Model):
slug = models.SlugField(max_length=255, unique=True)
vendor = models.ForeignKey(Vendor)
user = models.ForeignKey(User, blank=True, null=True)
product = models.ForeignKey(Product, blank=True, null=True)
images = models.ManyToManyField(ReviewImage, blank=True, null=True)
headline = models.CharField(max_length=100)
review = models.TextField(blank=True, null=True)
rating = models.IntegerField()
active = models.BooleanField(default=1)
created = models.DateTimeField(auto_now_add=True)
changed = models.DateTimeField(auto_now=True)
# This is the problem... I works, but no vendor is shown if there is no review.
vendor_list = (Vendor.objects.filter(category=category,
review__product__isnull=True,
active=True)
.annotate(rating_avg=Avg('review__rating')))
我如何使用review__product__isnull=True 做到这一点?如果根本没有review,我还是要那个vendor,但是评分应该是:“0”,..怎么办?
【问题讨论】:
-
您能详细说明您要做什么吗?似乎您试图在未经审查的情况下获得供应商,但 Avg 的使用令人困惑。
-
我想列出该类别中的所有供应商,并显示每个供应商的评级......但我只想要未选择“产品”的评级......感谢您的帮助!
-
@Secator:这是一个非常有用的答案(对于
COALESCE的使用和 Django 聚合的猴子补丁),但在这种情况下很难应用,因为需要OUTER JOIN在评论的产品领域。 (如果你知道用 Django 的查询语言表达这一点的好方法,我很乐意看到它。)
标签: python django django-queryset