Django模型迁移两个表

时间:2012-03-20 16:32:42

标签: python django django-models

我有一个评论网站,写在Django,..

如何迁移模型以获取审核数据 -

我有一个产品类和一个供应商类,最后是一个审查类,对产品和供应商进行审核:

class Product(models.Model):
    ....

class Vendor(models.Model):
    ....

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)

当我选择产品或供应商时,我需要从REVIEW课程中获得AVG评级...我该怎么办?

1 个答案:

答案 0 :(得分:2)

查看django docs on annotation

这样的事情应该有效:

from django.db.models import Avg
qs = Vendor.objects.annotate(rating_avg=Avg('review__rating'))

然后qs中的每个供应商都将拥有.rating_avg属性。