Search code examples
pythongoogle-app-enginegoogle-cloud-datastoregoogle-cloud-endpointsapp-engine-ndb

google app engine python: how to extend ndb User class


I'm using google cloud endpoints and want to be able to extend the User class so that a call to get_current_user would return an AppUser object with my own extra properties.

class AppUser(--?--): # what should i put here
    gcm = ndb.StringProperty()

    def send_notification(self):
         # do something with gcm ...
         pass

how can I implement such a thing ? and is there a better way ?


Solution

  • It's not supported officially, and I wouldn't recommend messing with it; there's a better way, just add a new Model and link it via the user_id, then you can do whatever you want with it.

    class Preferences(ndb.Model):
        gcm = ndb.StringProperty()
    
    user = users.get_current_user()
    Preferences(
        gcm='',
        id=user.user_id(),
    ).put()
    prefs = Preferences.get_by_id(user.user_id())