我試圖用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()正常工作。
嘗試將'print'放入您正在調用的模型函數中。這樣你就可以知道它的呼叫與否。 –
是的,它被稱爲。它在for循環中斷開。無法說明原因。 – ChrisDevWard