teatimer teatimer - 2 months ago 6
Python Question

Use variable to get property value

I want be able to get values from my database using a variable, the last line is what I'm trying to achieve.

class Data(db.Model):
property = db.StringProperty()

data = Data(property = 'value')
data.put()

query = db.GqlQuery("SELECT * FROM Data")

myvar = 'property'

for line in query:
line[myvar]

Answer

Thank you voscausa,

class Data(db.Model):
  property = db.StringProperty()

data = Data(property = 'value')
data.put()

query = db.GqlQuery("SELECT * FROM Data")

myvar = 'property'

for line in query:
  getattr(line, myvar)
Comments