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
Tagentities in aGroupentity’s entity group, theReferencePropertyprovides no real value. You could instead usetag_entity.key().parent()to get theGroupentity’s key ortag_entity.parent()to get theGroupentity itself. You could then use aListPropertyonGroupto 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
Groupentity is updated, have you considered using aListPropertyto store your tags on directly onGroupentity. Or, storing a list of tag key_names directly onGroup. Either approach would make fetching all tags for a group quite easy and efficient.