开发者

GAE organizing data structure problem

开发者 https://www.devze.com 2023-01-21 06:35 出处:网络
Ok. I\'m working with GAE. And i want create something like this: I have types \"group\" \"topic\" \"tag\":

Ok. I'm working with GAE. And i want create something like this:

I have types "group" "topic" "tag":

  1. each "group" can have as many "topics" as needed

  2. each "topic" can have as many "tags" as needed

  3. 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.S开发者_Go百科tringProperty(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')


The solution is to use a db.ListProperty with a type of db.Key. see: http://code.google.com/appengine/articles/modeling.html (section many to many)

0

精彩评论

暂无评论...
验证码 换一张
取 消