gpt4 book ai didi

python - NDB 使用 Users API 形成实体组

转载 作者:行者123 更新时间:2023-11-28 22:02:59 25 4
gpt4 key购买 nike

我正在努力思考一个看似非常简单的用例,但我似乎失败得很惨。该练习的目标是在高复制数据存储中查找使用 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。您可以像这样创建帐户:

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

你可以这样查找它们:

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

关于python - NDB 使用 Users API 形成实体组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10271103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com