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?

Answers


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".


Need Your Help

How do I establish a COM port on virtual machine that points at the USB port of the host desktop?

powershell serial-port virtual-machine hyper-v

I am using Hyper-V Manager on my Windows 8.1 device to host a virtual machine. As it turns out, there is no way to enable your COM ports through the user interface, instead you must use Windows

Posting JSON values without quotes not working with model binding in ASP.NET MVC Web Api OData controller

json asp.net-mvc-4 asp.net-web-api odata

I'm a bit perplexed by the behavior of my ODataController. I'm binding to an entity containing four properties - a nullable decimal, a string, a long and an array of longs. If any one of the values...