Search code examples
pythongoogle-app-enginewebapp2

associate temporary values with appengine model querysets


The following is my model:

I have two tables match and team:

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()

At runtime, when a post request is made to one of the handler functions, i want to dynamically associate a team rating with the queryset of Match and send the value, this is how i try to do:

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)

and i have a custom function to get fetch the rating from the current team, it is as follows:

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

Trouble: i get error in setattr in place of setattr i also tried m.rating1 = te1 and m.rating2 = te2 but even that does not seem to work. Everything else is working flawlessly.

Please help thanks!


Solution

  • The syntax is setattr(m, 'rating1', te1.rating1); but this is no different from m.rating1 = te1.rating1.

    May we see the trace back?