gpt4 book ai didi

python - GAE 模型 : How to list child nodes in parent

转载 作者:搜寻专家 更新时间:2023-10-30 20:31:17 25 4
gpt4 key购买 nike

在 Python 和引用中使用 Google App Engine,您可以自动获得从引用对象到您正在处理的对象的反向引用。这在找到的答案中描述得很好 here .

我想做的是创建一个(更简单的)一对多关系,每个组都有一个标签列表,每个标签只属于一个组。我把它想象成下面这样:

class Group(db.Model):
# All of my group-specific data here.

class Tag(db.Model):
text = db.StringProperty(required=True)
group = db.ReferenceProperty(Group, collection='tags')

假设我理解正确......在上面,你最终得到每个 Group 都有一个 tags 属性:

# Get the list of Tag objects that belong to a Group object
mytags = mygroup.tags

我的问题是,是否有一种“标准”方法可以将该信息包含在 Group 对象中?查看数据模型时,您无法通过查看 Group 对象来了解它具有适用于它的标签列表。我希望能够将 Group,Tags 定义为类似

class Group(db.Model):
# This should automatically be the same as the "tags" property that is
# created for the Group model by the definition of the Tag model
tags = db.ListProperty(db.Key)
# All of my group-specific data here.

class Tag(db.Model):
text = db.StringProperty(required=True)
group = db.ReferenceProperty(Group, collection='tags', required=True)
# Other tag specific information here (such as url, etc)

我的想法是,当我查看 Group 模型时,我希望能够看到它有一个 Tag 对象列表作为属性。我感觉 不自然 必须查看 Tag 模型才能了解此信息。

注意:可能值得注意的是,我计划让相关的 Group 成为父级(对于实体Group) 中的每一个都是 Tag。每当我修改一个 Group 时,我都会完全更新它,用新的替换所有的 Tag,我想为给定我正在查看的 Group 的“版本”。

我的数据的用例包括:

  • 更新组 - 创建或更新 6 个组。如果创建,则为组创建标签。如果更新,则完全替换组的标签(删除旧的)- 每 5 分钟左右发生一次
  • 读取最近的组 - 获取 6-20(尚未确定)最近修改的组,无需加载他们的标签
  • 读取单个组 - 获取单个组的信息,包括它的标签(每个组将有 10 到 20 个标签)

最佳答案

您打算如何查询您的标签,您是否只关心来自特定实体组的标签?如果是这种情况,由于您要将 Tag 实体放入 Group 实体的实体组中,因此 ReferenceProperty 不提供实际值.您可以改为使用 tag_entity.key().parent() 获取 Group 实体的键或使用 tag_entity.parent() 获取 Group 实体本身。然后,您可以在 Group 上使用 ListProperty 来存储标签实体的 key_name(我认为这将是实际的“标签”)。

由于每次 Group 实体更新时您都会替换所有组的标签,您是否考虑过使用 ListProperty 将您的标签直接存储在 上组实体。或者,将标签 key_names 列表直接存储在 Group 上。任何一种方法都会使获取一个组的所有标签变得非常容易和高效。

关于python - GAE 模型 : How to list child nodes in parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6232092/

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