2014-03-13 158 views
0

我試圖用HighCharts設置我的django管理頁面,這樣我可以讓管理員輕鬆地將一些數據可視化。模型內的Django過濾器

我目前可以獲得PeopleCount模型(totalPeople)中所有對象的車手總數,但是當我嘗試通過StopID(totalPeopleByStop)進行過濾時,它會中斷。

這裏是我的models.py,隨着PeopleCount類的上述方法一起:

from django.db import models 
from django.template.loader import render_to_string 

class Vehicle(models.Model): 
    VehID = models.AutoField(primary_key=True) 
    Title = models.CharField(max_length=40) 
    Driver = models.CharField(max_length=25) 

def __unicode__(self): 
    return self.Title 


class Location(models.Model): 
    LocID = models.AutoField(primary_key=True) 
    VehID = models.ForeignKey('Vehicle') 
    Latitude = models.DecimalField(max_digits=10, decimal_places=6) 
    Longitude = models.DecimalField(max_digits=10, decimal_places=6) 
    Speed = models.DecimalField(max_digits=4, decimal_places=1) 

    def __unicode__(self): 
     #VehID + LocID Identifier 
     return str(self.LocID) 


class PeopleCount(models.Model): 
    CountID = models.AutoField(primary_key=True) 
    StopID = models.ForeignKey('StopLocation') 
    VehID = models.ForeignKey('Vehicle') 
    LocID = models.ForeignKey('Location') 
    Date = models.DateField(auto_now_add=True, blank=False) 
    Time = models.TimeField(auto_now_add=True) 
    Count = models.IntegerField() 

    Date.editable = True 
    Time.editable = True 

    def totalPeople(self): 
     totPeople = 0 
     for model in PeopleCount.objects.all(): 
      totPeople += model.Count 
     return totPeople 

    def totalPeopleByStop(self, stopname): 
     totPeople = 0 
     name = stopname 
     for model in PeopleCount.objects.filter(StopID=stopname).all(): 
      totPeople += model.Count 
     return totPeople 

    def __unicode__(self): 
     return str(self.CountID) 

    def peoplecount_chart(self): 
     totalPeople = self.totalPeople() 
     totalRamsey = self.totalPeopleByStop("Ramsey") 
     lu = { 'categories' : [self.StopID],\ 
      'tot_riders' : [self.Count],\ 
      'tot_riders_at_stop' : [totalPeople]} 

     return render_to_string('admin/tracker/peoplecount/peoplecount_chart.html', lu) 
    peoplecount_chart.allow_tags = True 

class StopLocation(models.Model): 
    StopID = models.AutoField(primary_key=True) 
    StopName = models.CharField(max_length=40) 
    Latitude = models.DecimalField(max_digits=10, decimal_places=6) 
    Longitude = models.DecimalField(max_digits=10, decimal_places=6) 

    def __unicode__(self): 
     #VehID + LocID Identifier 
     return str(self.StopName) 

有不通過Django的或在任何日誌中出現的任何錯誤,所以我不能完全肯定如何讓totalPeopleByStop()正常工作。

+0

嘗試將'print'放入您正在調用的模型函數中。這樣你就可以知道它的呼叫與否。 –

+0

是的,它被稱爲。它在for循環中斷開。無法說明原因。 – ChrisDevWard

回答

1

您可以使用django aggregates稍微簡單一點。

from django.db.models import Sum 


class PeopleCount(models.Model): 
    ... 

    def totalPeopleByStop(self, stopname): 
     return PeopleCount.objects.filter(StopID=stopname).aggregate(
      total_people_by_stop=Sum('Count'))['total_people_by_stop'] 
+0

爲什麼新班'PeopleStop'?你的意思是'PeopleCount'? – ndpu

+0

是的,你是對的。對於那個很抱歉。 – schillingt

+0

此外,此方法在PeopleCount中作爲方法沒有多大意義。它應該只是一個實用程序或靜態類的功能。它根本沒有利用'''self'''。 – schillingt