谷歌应用引擎 python:如何扩展 ndb 用户类 [英] google app engine python: how to extend ndb User class

本文介绍了谷歌应用引擎 python:如何扩展 ndb 用户类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ?

推荐答案

官方不支持,不建议乱用;有一个更好的方法,只需添加一个新的 Model 并通过 user_id 链接它,然后你就可以用它做任何你想做的事.

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

这篇关于谷歌应用引擎 python:如何扩展 ndb 用户类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆