Sumeet Pareek
Sumeet Pareek

Reputation: 2819

Adding a user supplied property (at runtime) to an instance of Expando class in Google App Engine?

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.

Upvotes: 0

Views: 771

Answers (2)

S.Lott
S.Lott

Reputation: 391818

Usually, we use the setattr function directly.

setattr( entity_two, 'some_variable', some_value )

Upvotes: 3

Sumeet Pareek
Sumeet Pareek

Reputation: 2819

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

Upvotes: 0

Related Questions