开发者_如何学CIn my Main
model I have a total_value
and I sort with this value. When a user enters a value I wish to tell the user the order of his total_value
. How do I do this?
In other words, if total_value
s in database are 5,8,10,25 and a user enters a total_value
of 15 how do I tell him that he will be in the 4th place?
Thanks!
You need to write a query which counts the number of values smaller than the user's total_value
. This may be slow if there are many Main
entities. In that case, you may wish to only count the first N
values smaller, and then stop (e.g., tell the user they are ranked 1000+). Your query might look like this:
# get the rank of total_value compared to existing total_values (up to 1,000)
rank = Main.all().filter('total_value <', total_value).count(1000) + 1
The count-based solutions David and Rahul propose will only work efficiently for small numbers of entities. If you want to determine position inside a large list, you want something like the ranklist project.
You could get the count of number of entities that have total_value
less than or equal to the current users total_value
.
You could use the count() method to do so (on a Query object).
精彩评论