Python 模型内的Django过滤器

Python 模型内的Django过滤器,python,django,python-2.7,django-models,Python,Django,Python 2.7,Django Models,我正在尝试用HighCharts设置django管理页面,以便管理员能够轻松地可视化一些数据 我目前可以获得PeopleCount模型(totalPeople)中所有对象的乘客总数,但当我尝试按StopID(totalPeopleByStop)过滤时,它会中断 这是my models.py,以及PeopleCount类中的上述方法: from django.db import models from django.template.loader import render_to_string

我正在尝试用HighCharts设置django管理页面,以便管理员能够轻松地可视化一些数据

我目前可以获得PeopleCount模型(totalPeople)中所有对象的乘客总数,但当我尝试按StopID(totalPeopleByStop)过滤时,它会中断

这是my 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循环中中断。不知道为什么。为什么新建类
PeopleStop
?你是说
PeopleCount
?同样,这个方法作为PeopleCount中的一个方法没有多大意义。它应该只是一个实用程序或一个静态类函数。它根本没有利用
self
。@schillingt谢谢你的回复!这绝对是完成计数的更好方法。然而,原来的问题仍然存在。我已经修改了这两种方法来使用聚合,运行totalPeople工作得很好,但和以前一样,当我尝试使用totalPeopleByStop应用过滤器时,它失败了。我一直在使用intellij进行调试,当在totalPeopleByStop的返回方法和程序的下一行(peoplecount\u chart的返回方法)设置断点时,它只是跳过而不返回。有什么建议吗?在没有看到更新的代码的情况下,我能做的最好的事情就是告诉您将调用包装在一个try/Exception异常中为e,并将异常打印到控制台。这听起来像是引发了异常,但如果从模板中调用它,它将被静默。
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']