2013-04-25 73 views
0

我有以下型號:Django的生成摘要視圖

class Model1(models.Model):  
    name = models.CharField(max_length=255, null=True) 


class Model2(models.Model): 
    model1 = models.ForeignKey(Model1, null=True) 
    impressions = models.PositiveIntegerField(default=0) 
    clicks = models.PositiveIntegerField(default=0) 
    spend = models.PositiveIntegerField(default=0)  
    user = models.ForeignKey(User, null=True) 

我想創建它顯示每個MODEL1在用戶登錄廣告的數量與它關聯的視圖+的計數展示次數,點擊次數和支出。

快速樣機會是這樣:

=================================================== 
=Model1 Name=Model2 Count=Total Impressions=TotalClicks= 
= First =  10 =  1  =  1  = 
= Second =  6 =  100  =  20 = 
======================================================== 

我已經與每個MODEL1鍵多個值的字典的想法玩弄,但我無法弄清楚如何做到這一點。

任何幫助,將不勝感激

回答

0

在我去的字典結束存儲的列表計算字段如下:

data = {} 
for m1 in Model1.objects.all(): 
    list = [] 
    model2s = m1.model2_set.filter(user=request.user) 
    if model2s: 
     list.append(calculated field) 
     list.append(Another calculated field)  
     data[m1.name] = list 
1

類似下面應該努力讓你指的是字典:

data = {} 
for m1 in Model1.objects.all(): 
    data[m1.name] = Model2.objects.filter(model1=m1) 
+0

謝謝,這讓我走上了正軌,我發佈了我最終做的事情。 – 2013-04-27 15:02:12