1

我在我的 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',{})
4

1 回答 1

-1

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

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

于 2013-08-02T19:42:01.863 回答