在django manager中实现过滤器时的属性错误

时间:2013-08-02 17:41:14

标签: django pandas django-models django-managers

我在django项目中有一位经理,正在实施一个过滤器,如下所示 我启动django shell并得到此错误:

>>> from django.http import HttpRequest
>>> r=HttpRequest()
>>> r.session=()
>>> from movierating.models import *
>>> RatingModel.statManager.RatingTimeLine(r)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/data/dashapp/movierating/managers.py", line 27, in RatingTimeLine
(data,display,err)=self.getdatacache(request)
 File "/data/dashapp/movierating/managers.py", line 11, in getdatacache
filterDict=request.session.get('RatingFilter',{})
AttributeError: 'tuple' object has no attribute 'get'
>>> from django.http import HttpRequest
>>> r=HttpRequest()
>>> r.session()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'HttpRequest' object has no attribute 'session'r code here

文件manager.py如下所示:

import json,datetime
from django.db import models
from django.db.models import *
from pandas import *
from urllib import urlencode
import hashlib
from django.core.cache import cache
from web.extras import *

class RatingManager(models.Manager):
def getdatacache(self,request):
  filterDict=request.session.get('RatingFilter',{})
  (fapply,display,err)=normalizeFilter(self.model,filterDict)
  cache_id=urlencode(fapply.items())
  cache_id=hashlib.md5('RatingFilter'+cache_id).hexdigest()
  data=None
  if data==None:
     res=self.model.objects.order_by('date').filter(**fapply).values('date').annotate(
        RatingCounts = Count('rating'),
        RatingSum =  Sum('rating'),
     )

     data=DataFrame(list(res))
     data['AverageRating']=data['RatingSum']/data['RatingCounts']
     cache.set(cache_id,data)
  return (data,display,err)

def RatingTimeLine(self,request):
   jsondata={}
   jsondata['chartconfig']={}
   jsondata['chartconfig']['title']="Average Movie Rating per Day"
   jsondata['chartconfig']['names']=['AverageRating']
   (data,display,err)=self.getdatacache(request)
   jsondata['chartconfig']['errors']="<br/>".join(err)
   jsondata['chartconfig']['subtitle']="<br/>".join(display)
   jsondata['series']=data[['data','AverageRating']].values.tolist()
   data=json.dumps(jsondata,cls = SeriesEncoder)
   return {'data':data}

我的models.py中有这个模型:

class RatingModel(models.Model):
   movie=models.ForeignKey(MovieModel)
   user=models.ForeignKey(userModel)
   rating=models.IntegerField()
   date=models.DateField()
   objects=models.Manager() #default manager
   statManager=RatingManager() #new manager class
   # FilterMapping={
    #'movie':'movie__name', #django relational
    # }
def __unicode__(self):
   return "id: %d rating for movie is %s" %(self.id,self.movie.name) #relationships
class Meta:
   app_label = 'movierating'
   db_table = 'rating'

这一行可能出现的错误是什么?

filterDict=request.session.get('RatingFilter',{})

1 个答案:

答案 0 :(得分:-1)

在第三行中,您说r.session=() - 也就是说,您要为r.session分配一个空元组。您的代码稍后会尝试在此元组上调用get(),这不是受支持的操作。

我不确定你在这里要做什么。 request.session通常由会话中间件填充。如果您尝试以编程方式测试代码,可能需要查看Django测试框架,supports sessions