What is it?
This is a way to add tags functionality to classes derived from
Google App Engine by way of a mixin.
Why is it important?
Adding tags to data models is a popular phenomenon, but there does not appear to be a published way to accomplish the common functionality with
. This method ought to be reusable and performant.
How does it work?
Get the full gist is gist.github.com.
This concept has two
The requirements here for me were as follows:
- The tags be stored with the model (i.e. can be accessed without “joins”)
instances be updated automatically when a model is updated
- The updates apply only on changed
- The update occur in parallel
I chose to store tags on the mixin as a repeated
, with a separate
model whose key can be generated from the tag (i.e. the tag
instances are updated in parallel by
model hooks, as seem below.
class that keeps track of the popularity (
) of a
tag and linked items.
The mixin that one would use to add tags to models is as follows:
Given a model with the mixin like this:
It is straightforward to add for example getting recently updated or old tags
The unit tests in the associated gist
illustrate usage and expectations in better detail.
Careful Be mindful that the mixin uses the hooks
. If your models also use these you will need a way to call them
e.g. calling from your hooks the respective
corresponds to the index
. One could call the hooks for
all immediate ancestors with something like this:
If the hooks are buried deeper than immediate ancestors one would have to recurse through
Code above is licensed under the MIT https://brianmhunt.mit-license.org/ license.