GAE模型:如何列出父节点中的子节点 [英] GAE Models: How to list child nodes in parent

查看:108
本文介绍了GAE模型:如何列出父节点中的子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python和引用中使用Google App Engine,您会自动获得从引用对象到您正在处理的引用对象的反向引用。在此处找到的答案中对此进行了很好的描述。



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

  class Group(db.Model):
#具体数据。

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

假设我正确理解了一切...在上面,你结束每个具有标签的群组

  #获取属于Group对象的Tag对象列表
mytags = mygroup.tags

我的问题是,是否有一个标准的方式来包括该信息在对象?当查看数据模型时,您不能通过查看 Group 对象,它有一个适用于它的标签列表。我想能够将 Group,Tags 定义为

  class Group(db.Model):
#这应该与标签属性相同,即
#通过标签模型的定义为组模型创建
tags = db.ListProperty(db.Key)
#我的所有组特定数据。

class Tag(db.Model):
text = db.StringProperty(required = True)
group = db.ReferenceProperty(Group,collection ='tags',required = True)
#此处的其他标记特定信息(例如url等)

当我查看Group模型时,我想要看到它有一个Tag对象列表作为属性。

注意:注意: strong>可能值得注意的是,我计划让 Group 成为其每个标签的父类(对于实体组) / code> s。任何时候我修改 Group ,我将完全更新它,用新的替换它的所有标签并且我想为 Group 的给定版本获得正确的列表。我正在查看。



我的数据的用例包括:




  • 更新组 - 创建或更新6组。如果创建,则为组创建标记。如果更新,请完全替换群组的标签(删除旧标签) - 每5分钟左右发生一次

  • 阅读最近的群组
  • 读取单个组 - 获取单个组的信息,包括其标签(每个组都包含标签)将有10到20个标签)

解决方案

你只关心来自特定实体组的标签吗?如果是这样,由于您要将 Tag 实体放在 Group 实体的实体组中, ReferenceProperty 不提供实际值。您可以使用 tag_entity.key()。parent()获取实体的键或 tag_entity.parent()以获取实体本身。然后,您可以在上使用 ListProperty 来存储标记实体的key_names(我假设它将是实际'



由于您随时会更新 Group 实体,您已考虑使用 ListProperty 将您的代码直接存储在 Group 实体上。或者,将标签key_names的列表直接存储在 Group 上。任何一种方法都可以提取一个组的所有标签很容易和高效。


Using Google App Engine in Python and references, you automatically get a back reference from the referenced object to the one you're dealing with. This is described very well in the answer found here.

What I'd like to do is create a (simpler) one-to-many relationship, with each Group having a list of Tags, and each Tag belongs to only one Group. I picture it something like the following:

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')

Assuming I understand everything correctly... In the above, you wind up with each Group having a tags property:

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

My question is, is there a "standard" way to include that information in the Group object? When looking at the data models, you can't see by looking at the Group object that it has a list of Tags that apply to it. I'd like to be able to define Group,Tags as something like

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)

The idea being that I want to be able to see, when I look at the Group model, that it has a list of Tag objects as a property. It feels unnatural to me to have to look at the Tag model to know this information.

Note: It may be worth noting that I plan on having the Group in question be the parent (for the Entity Group) of each of it's Tags. Any time I modify a Group, I will be updating it completely, replacing all of it's Tags with new ones, and I want to get the correct list of them for the given "version" of the Group I'm looking at.

The use cases for my data include:

  • Update groups - create or update 6 groups. If create, then create the tags for the group. If update, completely replace the tags for the group (removing old ones) - happens every 5 minutes or so
  • Read recent groups - get the 6-20 (undecided yet) most recently modified groups, no need to load their tags
  • Read single group - get the information for a single group, including it's tags (each one will have between 10 and 20 tags)

解决方案

How are you going to query your tags, will you be concerned only with tags from a particular entity group? If that is the case, since you're going to put your Tag entities in a Group entity's entity group, the ReferenceProperty provides no real value. You could instead use tag_entity.key().parent() to get the Group entity's key or tag_entity.parent() to get the Group entity itself. You could then use a ListProperty on Group to store the key_names of the tag entities (which I presume will be the actual 'tag').

Since you are replacing all of a group's tags any time the Group entity is updated, have you considered using a ListProperty to store your tags on directly on Group entity. Or, storing a list of tag key_names directly on Group. Either approach would make fetching all tags for a group quite easy and efficient.

这篇关于GAE模型:如何列出父节点中的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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