将临时值与appengine模型查询集相关联

时间:2012-04-14 18:34:07

标签: python google-app-engine webapp2

以下是我的模特:

我有两张表matchteam

class Match(DictModel):
    date_time = db.DateTimeProperty()
    team1 = db.StringProperty()
    team2 = db.StringProperty()
    venue = db.StringProperty()
    result = db.IntegerProperty()

class Team(DictModel):
    tslug = db.StringProperty()
    name = db.StringProperty()
    matches_played = db.IntegerProperty()
    matches_won = db.IntegerProperty()
    rating = db.FloatProperty()

在运行时,当对其中一个处理函数发出post请求时,我想动态地将团队评级与Match的查询集关联并发送值,这是我尝试的方法:< / p>

matches = Match.all()
      matches.filter('date_time <=', end)
      matches.filter('date_time >=', start)
      match_dict = functs.create_dict(matches)
      self.response.out.write(match_dict)

我有一个自定义函数来获取当前团队的评级,它如下:

def to_dict(self):
       return dict([(p, unicode(getattr(self, p))) for p in self.properties()])

def create_dict(matches):
  lst = []
  for m in matches:
    t1 = m.team1
    t2 = m.team2
    te1 = Team.all().filter("name =", t1).get()
    te2 = Team.all().filter("name =", t2).get()
    m.setattr('rating1', te1.rating)
    m.setattr('rating2', te2.rating)
    lst.append(m)
  data_dict = json.dumps([l.to_dict() for l in lst])
  return data_dict

麻烦:我在setattr代替setattr我收到了错误我还尝试了m.rating1 = te1m.rating2 = te2但是即使这样也没有工作。其他一切都完美无缺。

请帮助谢谢!

1 个答案:

答案 0 :(得分:0)

语法为setattr(m,'rating1',te1.rating1);但这与m.rating1 = te1.rating1没有区别。

我们可以看到回溯吗?