2012-03-20 72 views
0

我有一個評論網站,在Django writen ..Django的模型遷移兩個表

我如何遷移模式獲得審覈數據 -

我有一個產品類別和供應商類別,並finaly條評論類產品和廠商:

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) 

當我選擇一個產品或供應商,我需要從複習課的AVG的評級...我怎麼可以這樣做?

回答

2

查看django docs on annotation

像這樣的東西應該工作:

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

然後在qs每個供應商將有一個.rating_avg財產。

+0

對不起,修正了一些錯誤。 – dgel 2012-03-20 17:02:22

+0

vendor_list = Vendor.objects.filter(category = category,review__product__isnull = True,active = True).annotate(rating_avg = Avg('review__rating'))如何使用「review__product__isnull = True」做到這一點?如果沒有審查,就沒有供應商......該怎麼辦? – pkdkk 2012-03-20 20:56:35