Google App Engine ndb: find the position of an order query list

江枫思渺然 提交于 2019-12-11 03:38:49

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/14493479/google-app-engine-ndb-find-the-position-of-an-order-query-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!