Can I make an Appengine model property call a function?
-
22-09-2019 - |
Question
I'm implementing a frontpage with "hot" stories based on a certain ranking algorithm. However, I can't figure out how to pass Appengine's 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 Appengine's datastore models?
OTHER TIPS
I don't think this is possible with App Engine the way you describe it, but I think it is possible to achieve what you want. You want the datastore to run your ranking function against every element in the datastore, every time you do a query. That is not very scalable, as you could have millions of entities that you want to rank.
Instead, you should just have a integer property called rank, and set it every time you update an entity. Then you can use that property in your order clause.