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

时间:2013-06-23 12:45:14

标签: 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()

但这看起来非常原始。如何创建这样的字段,其中唯一性是一个重要因素,那么如何在Google应用引擎中创建用户,就像最佳做法一样。 此外,您如何将用户链接到博客帖子,因为谷歌数据存储中不允许链接。 最后是否有一个管理所有迁移的应用程序?像GAE的南方?似乎有一种迁移方式,但它涉及大量的boiler-plate code

另外,我来自django背景,所以我发现所有这些都有点反直觉,因为这不是关系数据库。

1 个答案:

答案 0 :(得分:2)

您的User课程现在没问题,您可以在开发过程中根据需要为模型添加字段。但您应该使用ndb而不是dbndb处理更多功能(如自动缓存)。您可以找到更多详细信息in the documentation

如果您想了解更多高级用户或数据存储模型,可以查看已经处理用户登录和注册的gae-boilerplate(即使是来自facebook / twitter / ...)。 README有很好的文档记录: 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检索用户发布的所有博客帖子。