NDB使用用户API来组成一个实体组 [英] NDB using Users API to form an entity group

查看:92
本文介绍了NDB使用用户API来组成一个实体组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图围绕似乎是一个非常简单的用例,但我似乎失败了。练习的目标是查找一组用于在高复制数据存储中使用Google帐户用户名登录的记录,并且保持非常一致。

我的数据如下所示:

  class Account(ndb.Model):
owner = ndb.UserProperty()
name = ndb.StringProperty()

class Content(ndb.Model):
content = ndb.StringProperty()

当我第一次创建账户时,我只是做了以下操作:

  current_user = users.get_current_user()
new_account = Account(owner = current_user,name ='Some Name')
new_account.put()

现在创建内容:

  new_content = Content(parent = new_account.key,content ='Some Content')
new_content.put()

当用户登录,我只能通过UserProperty查询,但我似乎无法将用户设置为父级对于一个实体组,我如何确保我始终可以通过登录用户查找帐户并保持非常一致?



一旦我拥有该帐户,祖先查询过程将确保内容检索的一致性,但是我坚持要根据用户计算出祖先。 解决方案

最简单也是最好的方法是使用键名。在这种情况下,您的帐户实体的密钥名称应该是用户的ID。你可以像这样创建账户:
$ b $ pre $ newcacount = Account(owner = current_user,id =一些名字')

你可以像这样查看它们:

  existing_account = Account.get_by_id(current_user.user_id())


I'm trying to wrap my head around what seems to be a very simple use case, but I seem to be failing miserably. The goal of the exercise is to look up a set of records for the user that logs in using the Google Accounts username within the high replication datastore and be extremely consistent.

My data looks like this:

class Account(ndb.Model):
    owner = ndb.UserProperty()
    name = ndb.StringProperty()

class Content(ndb.Model):
    content = ndb.StringProperty()

When I first create the account, I just do the following:

current_user = users.get_current_user()
new_account = Account(owner=current_user, name='Some Name')
new_account.put()

Now create content:

new_content = Content(parent=new_account.key, content='Some Content')
new_content.put()

When the user logs in, I can only query by UserProperty, but I seem to be unable to set user as the parent for an entity group, so how do I make sure that I always can look-up the Account by the logged in user and be extremely consistent?

Once I have the account, the ancestor query process will ensure consistency for content retrieval, but I'm stuck on figuring out ancestry based on User.

解决方案

The easiest and best way to do this is using key names. In this case, the key name for your Account entity should be the ID of the user. You can create accounts like this:

new_account = Account(owner=current_user, id=current_user.user_id(), name='Some Name')

And you can look them up like this:

existing_account = Account.get_by_id(current_user.user_id())

这篇关于NDB使用用户API来组成一个实体组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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