【发布时间】:2014-03-13 09:20:28
【问题描述】:
我正在尝试使用 HighCharts 设置我的 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 循环内中断。不知道为什么。
标签: python django python-2.7 django-models