Filtering across model classes
I have the following model, where different texts are tagged to different collections of texts, and each text can have several versions, but only one "active" version:
class Collection(db.Model): name = db.StringProperty() ... class Text(db.Model): title = db.StringProperty(default="Untitled") ... class Version(db.Model): text = db.ReferenceProperty(Text, collection_name="versions") content = db.TextProperty() active = db.BooleanProperty(default=True) ... class Tag(db.Model): collection = db.ReferenceProperty(Collection, collection_name="c_tags") text = db.ReferenceProperty(Text, collection_name="t_tags")
Now I would like to display all the "active" texts in one collection:
class ViewCollection(webapp.RequestHandler): def(get): collection = Collection.get(self.request.get("key")) # grabs the collection to display tags = collection.t_tags # grabs all the tags linking a text and a collection texts = [t.text for t in tags]
This will give me a list of all the texts, so I can easily print all the text titles (t.title for t in texts), but is there any clever way I can now grab just the corresponding "active" version of each text?
Or is this type of filtering across different model classes impossible?
You have to issue another query on Version objects where text == t.text and active == True.
Depending on your use case, it might be worthwhile to denormalize and store the latest version in the Tag as well, so you don't need extra queries to get the data. This denormalization is a common "optimization" for nosql datastores.
App Engine's Datastore is not designed for this kind of relational algebra problems. I don't believe that with your current design it can be done efficiently. I suggest you read on NoSQL database concepts and redesign your models to be "less relational".