Using Property Builtin with GAE Datastore's Model

Posted by ejel on Stack Overflow See other posts from Stack Overflow or by ejel
Published on 2010-02-20T18:02:58Z Indexed on 2010/04/24 16:03 UTC
Read the original article Hit count: 265

I want to make attributes of GAE Model properties. The reason is for cases like to turn the value into uppercase before storing it. For a plain Python class, I would do something like:

Foo(db.Model):
    def get_attr(self):
       return self.something
    def set_attr(self, value):
       self.something = value.upper() if value != None else None
    attr = property(get_attr, set_attr)

However, GAE Datastore have their own concept of Property class, I looked into the documentation and it seems that I could override get_value_for_datastore(model_instance) to achieve my goal. Nevertheless, I don't know what model_instance is and how to extract the corresponding field from it.

Is overriding GAE Property classes the right way to provides getter/setter-like functionality? If so, how to do it?

Added:

One potential issue of overriding get_value_for_datastore that I think of is it might not get called before the object was put into datastore. Hence getting the attribute before storing the object would yield an incorrect value.

© Stack Overflow or respective owner

Related posts about google-app-engine

Related posts about google-datastore