teatimer teatimer - 2 months ago 20
Python Question

Datastore set property name from variable

I have an unknown variable that I want to use as a datastore property name. I'm using Expando, as I know you can dynamically create properties without first declaring them in the db class, however I am unable to do this as the property names are not known. I get the error: 'StoreNames' object does not support item assignment. Is there any way around this?

class StoreNames(db.Expando):
index = db.FloatProperty()

name = "unknown"

value = "something"

store = StoreNames()

store[name] = value
store.index = 0

Answer

Solved by using the following code:

class StoreNames(db.Expando):
  index = db.FloatProperty()

name = "unknown"

value = "something"

store = StoreNames()

setattr(db, name, value)

I would have answered earlier but Stackoverflow wouldn't let me. Thanks Brent Washburne