2014-03-13 8 views
0

관리자가 HighCharts로 장고 관리 페이지를 설정하려고하므로 관리자가 일부 데이터를 쉽게 시각화 할 수 있습니다.모델 내의 장고 필터

현재 PeopleCount 모델 (totalPeople) 내의 모든 개체에 대한 라이더의 총 수를 얻을 수 있지만 StopID (totalPeopleByStop)로 필터링하려고하면 끊어집니다. 여기

는 PeopleCount 클래스의 상기 방법과 함께, 내 models.py입니다 :

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) 

이 장고를 통해 또는 로그에서 발생하는 오류가없는, 그래서 나는 완전히 확실하지 않다 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의 메서드로는별로 중요하지 않습니다. 유틸리티 또는 정적 클래스 함수 여야합니다. '''''''을 전혀 이용하지 않고있다. – schillingt