pythongoogle-app-enginegoogle-cloud-datastoregqlgqlquery

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]

Solution

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