Exposing a "dumbed-down", read-only instance of a Model in GAE

Posted by Blixt on Stack Overflow See other posts from Stack Overflow or by Blixt
Published on 2010-05-02T19:52:10Z Indexed on 2010/05/02 19:58 UTC
Read the original article Hit count: 188

Filed under:
|
|

Does anyone know a clever way, in Google App Engine, to return a wrapped Model instance that only exposes a few of the original properties, and does not allow saving the instance back to the datastore?

I'm not looking for ways of actually enforcing these rules, obviously it'll still be possible to change the instance by digging through its __dict__ etc. I just want a way to avoid accidental exposure/changing of data.

My initial thought was to do this (I want to do this for a public version of a User model):

class ReadOnlyUser(db.Model):
    display_name = db.StringProperty()

    @classmethod
    def kind(cls):
        return 'User'

    def put(self):
        raise SomeError()

Unfortunately, GAE maps the kind to a class early on, so if I do ReadOnlyUser.get_by_id(1) I will actually get a User instance back, not a ReadOnlyUser instance.

© Stack Overflow or respective owner

Related posts about google-app-engine

Related posts about python