Google App Engine ndb: find the position of an ord

2019-07-15 13:21发布

On Google App Engine's ndb, I used the following to retrieve all entities and sort them according to their grade:

ranks = Member.query().order(-Member.grade)

Then I would like to know the position of a specific member:

i = 0
for rank in ranks:
    if rank.account == 'abc'
        position = i
        break
    i += 1

My question: is there an equivalent ndb operation to find the position of a specific entity? Thanks.

2条回答
虎瘦雄心在
2楼-- · 2019-07-15 13:59

It looks like you are trying to find the rank of a member. Take a look at http://code.google.com/p/google-app-engine-ranklist/ which will enable you to find the rank, with a query, without needing to loop through all the scores.

查看更多
手持菜刀,她持情操
3楼-- · 2019-07-15 14:03

I believe it could be done in two steps

  1. Retrieve the entry whose account is 'abc'

    target = Member.query(Member.account=='abc').get()

  2. Query for entries with higher grade

    n = Member.query(Member.grade>target.grade).count()

Then you get target's rank as n+1

查看更多
登录 后发表回答