【问题标题】:Django filter from within models模型中的 Django 过滤器
【发布时间】: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


【解决方案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']

【讨论】:

  • 为什么是新课程PeopleStop?你是说PeopleCount
  • 此外,此方法作为 PeopleCount 中的方法没有多大意义。它应该只是一个实用程序或静态类函数。它根本没有利用self
  • @schillingt 感谢您的回复!这绝对是完成计数的更好方法。然而,原来的问题仍然存在。我已经修改了这两种方法以使用聚合,运行 totalPeople 工作正常,但和以前一样,当我尝试使用 totalPeopleByStop 应用过滤器时,它失败了。我一直在使用 intellij 进行调试,并且在 totalPeopleByStop 的返回方法和程序的下一行(peoplecount_chart 的返回方法)设置断点时,它只是跳过并且不返回。有什么建议吗?
  • 在没有看到更新代码的情况下,我能做的最好的事情就是告诉您将调用包装在 try/except Exception as e 中并将异常打印到控制台。听起来好像引发了异常,但如果从模板中调用它,它将被静音。
猜你喜欢
  • 2012-03-13
  • 2013-02-02
  • 2021-03-19
  • 2013-06-03
  • 1970-01-01
  • 2023-03-10
  • 2017-06-22
  • 2013-03-16
相关资源
最近更新 更多