谷歌应用引擎python:如何扩展ndb用户类

时间:2021-10-18 23:14:13

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.

我正在使用谷歌云端点,并希望能够扩展User类,以便调用get_current_user将返回具有我自己的额外属性的AppUser对象。

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 ?

我怎么能实现这样的事情呢?还有更好的方法吗?

1 个解决方案

#1


2  

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.

它没有正式支持,我不建议搞乱它;有一个更好的方法,只需添加一个新模型并通过user_id链接它,然后你可以用它做任何你想做的事情。

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

#1


2  

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.

它没有正式支持,我不建议搞乱它;有一个更好的方法,只需添加一个新模型并通过user_id链接它,然后你可以用它做任何你想做的事情。

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