0

By creating datastore models that inherit from the Expando class I can make my model-entities/instances have dynamic properties. That is great! But what I want is the names of these dynamic properties to be determined at runtime. Is that possible?

For example,

class ExpandoTest (db.Expando):
 prop1 = db.StringProperty()
 prop2 = db.StringProperty()

entity_one = ExpandoTest()
entity_two = ExpandoTest()

# what I do not want
entity_one.prop3 = 'Demo of dynamic property'

# what I want
entity_two.<property_name_as_entered_by_user_at_runtime> = 'This
property name was entered by the user, Great!!'

Is this possible? If so, how? I've already tried several ways to do this but didn't succeed.

Thanks in advance.

Ryan A.
  • 411
  • 4
  • 13
Sumeet Pareek
  • 2,739
  • 2
  • 22
  • 17

2 Answers2

3

Usually, we use the setattr function directly.

setattr( entity_two, 'some_variable', some_value )
S.Lott
  • 384,516
  • 81
  • 508
  • 779
0

Just found the solution to my own question. It was really simple but as I am a python noob I ended up posting the question that you see above.

For the code sample that I had used, this is what needs to be done:

entity_two.__setattr(some_variable, some_value) #where some_variable is populated by user at runtime :)
Sumeet Pareek
  • 2,739
  • 2
  • 22
  • 17