【问题标题】:google app engine python: how to extend ndb User class谷歌应用引擎python:如何扩展ndb用户类
【发布时间】:2015-05-15 08:20:51
【问题描述】:

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

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

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

我怎样才能实现这样的事情?有没有更好的方法?

【问题讨论】:

    标签: python google-app-engine google-cloud-datastore google-cloud-endpoints app-engine-ndb


    【解决方案1】:

    官方不支持,不建议乱用;还有一个更好的方法,只需添加一个新的Model 并通过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())
    

    【讨论】:

    • 但是如果user_id() 返回None 呢?
    • 相应地分支你的代码,如果它是None,那么不需要检索Preferences;我会在用户注册时创建该实体,这样您就可以确保它在每次后续登录时都存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多