GAE组织数据结构问题 [英] GAE organizing data structure problem
问题描述
好的。我正在与GAE合作。我想创建这样的东西:
我有类型grouptopictag:
> 每个群组可以根据需要拥有多少 每个主题可以根据需要添加尽可能多的标签
个主题
。
现在我有这样的:
class TopicGroup (db.Model):
title = db.StringProperty(required = True)
class Topic(db.Model):
title = db.StringProperty
group = db.ReferenceProperty(TopicGroup,'group','topics',required = True)
class TopicTag(db.Model):
topic = db.ReferenceProperty
group = db.ReferenceProperty(TopicGroup,'group','tags',required = True)
value = db.StringProperty(required = True)
但这不好(在我的模型主题只能有一个标签,我已经有一个裂缝在我的头...是否有人可以帮助?
>
多对多连接可以实现为连接表(或关系模型)。在下面的解决方案中,有一个模型包含应用于个人组
( GroupTags
)的所有标签和一个保存应用于 Topics
( TopicTags
)的标签的模型。
从标签
的引用中解除标签
本身允许您执行更改拼写标签,而不需要更新每个标签 c 或
主题
code>。
此外,该模型充分利用了AppEngine对具有引用它们的其他实体的实体创建自动反向引用的事实。在下面的模型中,一个Group实体将有一个名为 topics
的属性,它将获取所有 Topic
实体组
的引用指向
组
。类似地,每个 Group
从 GroupsTags
获取标签
模型,它给它属于它的所有标签
实体。每个标签
实体获得一个组
和主题
属性
这是一个非常强大的函数,它是一个非常强大的函数,
class Group(db.Model):
#我的所有群组特定数据。
class Topic(db.Model):
title = db.StringProperty(required = True)
group = db.ReferenceProperty(Group,collection ='topics')
#其他主题特定数据。
class Tag(db.Model):
text = db.StringProperty(required = True)
class GroupTags(db.Model):
group = db.ReferenceProperty(Group,collection ='tags')
tag = db.ReferenceProperty(Tag,collection ='groups')
class TopicTags(db.Model):
topic = db.ReferenceProperty(Topic,collection ='tags')
tag = db.ReferenceProperty(Tag,collection ='topics')
Ok. I'm working with GAE. And i want create something like this:
I have types "group" "topic" "tag":
each "group" can have as many "topics" as needed
each "topic" can have as many "tags" as needed
each "group" can have as many "tags" as needed
it's something like circle.
right now i have something like this:
class TopicGroup(db.Model):
value = db.StringProperty(required=True)
class Topic(db.Model):
title = db.StringProperty(required=True)
group = db.ReferenceProperty(TopicGroup, 'group', 'topics', required=True)
class TopicTag(db.Model):
topic = db.ReferenceProperty(Topic, 'topic', 'tags', required=True)
group = db.ReferenceProperty(TopicGroup, 'group', 'tags', required=True)
value = db.StringProperty(required=True)
but this is no good (in my model "topic" can have only one tag, but i nead "topic" to have as many tags as needed)
well i already have a crack in my head... is there someone who could help?
Many-to-many joins can be implemented as join tables (or Relationship Models). In the solution below, there is a model that holds all of the tags that are applied to individual Groups
(GroupTags
) and a model that holds the tags that are applied to Topics
(TopicTags
).
Decoupling the Tag
itself from references to the Tag
allows you to do things like change the spelling of a tag without needing to update every Group
or Topic
to which that Tag
is applied.
Also, this model takes good advantage of the fact that AppEngine creates automatic backreferences on entities that have other entities referencing them. In the models below, a Group entity will have a property called topics
that is a query that will fetch all Topic
entities who's group
reference points to that Group
. Similarly, each Group
gets a tags
property from the GroupsTags
model that gives it all of the Tag
entities that belong to it. Every Tag
entity gets a groups
and topics
property that is a query for all of the entities of those types that have the give Tag assigned, making searching by tag (a very typical operation) quite simple.
It's a very powerful and flexible approach to modeling systems such as yours.
class Group(db.Model):
# All of my group-specific data here.
class Topic(db.Model):
title = db.StringProperty(required=True)
group = db.ReferenceProperty(Group, collection='topics')
# other topic-specific data here.
class Tag(db.Model):
text = db.StringProperty(required=True)
class GroupTags(db.Model):
group = db.ReferenceProperty(Group, collection='tags')
tag = db.ReferenceProperty(Tag, collection='groups')
class TopicTags(db.Model):
topic = db.ReferenceProperty(Topic, collection='tags')
tag = db.ReferenceProperty(Tag, collection='topics')
这篇关于GAE组织数据结构问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!