App Engine Python - Sort db then put() index
Question: In my app, users earn a score and their details get stored in the datastore.
When the user logs in, I want to show their rank among all users(basically how
far away from the top score they are). So my solution was to sort the users'
profiles in descending order the put the index+1 to the Profile model and run
it in a cron.

However the cron fails. Any help or advise on a better way would be
appreciated:

    
    
        from google.appengine.ext import db
    
        def universal_rank(self):
            users = Profile.all().filter('leaderboard =', l.key()).order('-score')
            rank = 0
            for user in users:
                rank = rank + 1
                user.rank = rank
    
            db.put(users)
    

I'm using webapp2


Answer: I believe you have the wrong syntax for the filter query. From the docs,

<https://docs.djangoproject.com/en/1.8/topics/db/queries/>

It looks like what you need is something like this..

`users = Profile.objects.all().filter('leaderboard =',
l.key()).order('-score')`

Or, really, I don't think you need to the `.all()` in general.

`users = Profile.objects.filter('leaderboard =', l.key()).order('-score')`

I'm also not positive as to where you are getting `l.key()` but if you are
getting all users, I think that you could just do

`users = Profile.objects.all().order('-score')`

