Search code examples
google-app-enginedynamicgoogle-cloud-datastoreranking

Can I make an App Engine model property call a function?


I'm implementing a frontpage with "hot" stories based on a certain ranking algorithm. However, I can't figure out how to pass App Engine Datastore my own sort function (like I can in Python with sort(key=ranking_function)). I want something like this:

class Story(db.Model):
    user = db.ReferenceProperty(User)
    text = db.TextProperty()
    def ranking(self):
        # my ranking function, returns an int or something
        return 1
    ranking = property(ranking_function)

So that I can later call:

Story.all().order("ranking").limit(50)

Any idea how to do this using App Engine Datastore models?


Solution

  • There's no built in property that handles this, but there's a library, aetycoon, that implements DerivedProperty and other related properties that do what you want. Here's an article on how it works.