I’ve got two models: Group and Item. An Item has a list of tags and belongs to a Group.
class Group(db.Model):
name = db.StringProperty()
class Item(db.Model):
title = db.StringProperty()
tags = db.StringListProperty()
group = db.ReferenceProperty(Group)
So far, typical actions are adding a tag to an Item, removing a tag from an Item, and showing all Items matching a given Group and tag.
What’s a good way for getting a list of all unique tags used within a Group?
Ideally I’d like to have a property in Group that reflects the tags used:
class Group(db.Model):
name = db.StringProperty()
aggregated_tags = db.StringListProperty()
It would be even better if this included the number of Items that have this tag.
Permanent consistency is not a requirement, i.e. it is fine if the aggregated list of tags does not match the actual list of tags in use, as long as they become consistent eventually.
Item and Group are not in the same entity group, so I can’t have a transaction that updates the Item and the Group at the same time.
The best way to do this is to maintain it yourself. Update the list of tags whenever you add or remove one from an item. You can do this in a task queue task if you want to do it asynchronously.
Alternatively, you could write a mapreduce that you run periodically that recalculates the tag set for every group – in fact, this is pretty much a classic use-case for mapreduce.