python - 在 Google App Engine 中创建用户并以简单的方式进行迁移

标签 python google-app-engine google-cloud-datastore

我对 GAE 有点陌生,所以我想问一下,如何为像博客这样微不足道的东西创建用户模型。

到目前为止,这就是我所得到的:

class User(db.Model):  # Todo create a user model, that works with your login page
    username = db.UserProperty()
    password = db.StringProperty()

但这看起来很原始。如何在谷歌应用程序引擎中创建用户,就像创建这样的领域的最佳实践一样,其中唯一性是一个重要因素。 另外,您如何将用户链接到博客文章,因为谷歌数据存储中不允许链接。 最后有一个应用程序可以管理您的所有迁移吗?喜欢GAE 的南方吗?似乎有一种迁移的方法,但是涉及大量boiler-plate code .

此外,我有 django 背景,所以我发现这一切都有点违反直觉,因为这不是关系数据库。

最佳答案

您的 User 类目前没问题,您可以在开发过程中根据需要将字段添加到模型中。但您应该使用 ndb 而不是 db。 ndb 处理更多功能(例如自动缓存)。您可以查看更多详情in the documentation .

如果您想了解更高级的用户或数据存储模型,您可以查看 gae-boilerplate,它已经处理用户登录和注册(甚至来自 facebook/twitter/...)。自述文件有很好的文档记录: https://github.com/coto/gae-boilerplate

您可以在模型中实现某种关系,方法是使用 KeyProperty() 或在创建父实体时设置父实体。例如:

class BlogPost(ndb.Model):
    title = ndb.StringProperty()
    content = ndb.TextProperty()
    timestamp = ndb.DateTimeProperty(auto_add_now=True)

# Then you can specify a parent in the constructor (where user is a User entity)
blog_post = BlogPost(parent=user)
blog_post.title = 'Post title'
blog_post.content = 'Post content'
blog_post.put()

然后使用 Ancestor queries检索用户发布的所有博客文章。

关于python - 在 Google App Engine 中创建用户并以简单的方式进行迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17260865/

相关文章:

python - 如何使列表理解更具可读性?

python - 试图在 R 中定义一个函数,但结果却是对象

google-app-engine - Guice 和 JSF 2

google-app-engine - 关于获取datastore的Cursor行为的问题

python - uWSGI 信号框架 : signal to ALL workers being sent to the first available workers

python - 使用 webpy 线程化特定数据

google-app-engine - 从 ManagedVM 中公开多个端口

google-app-engine - 使用 App Engine 进行 Go 的 I18n 策略

java - 从数据存储中检索实体的时间

python - 使用grpc和cloud-datastore时如何修复App Engine Flex中的AttributeError?