[英]Google App Engine: how to tell sort location for a user value?
In my Main
model I have a total_value
and I sort with this value. 在我的Main
模型中,我有一个total_value
,并以此值排序。 When a user enters a value I wish to tell the user the order of his total_value
. 当用户输入值时,我希望告诉用户其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? 换句话说,如果数据库中的total_value
是5、8、10、25并且用户输入的total_value
为15,我如何告诉他他将排在第四位?
Thanks! 谢谢!
You need to write a query which counts the number of values smaller than the user's total_value
. 您需要编写一个查询,该查询计算的值的数量小于用户的total_value
。 This may be slow if there are many Main
entities. 如果有很多Main
实体,这可能会很慢。 In that case, you may wish to only count the first N
values smaller, and then stop (eg, tell the user they are ranked 1000+). 在这种情况下,您可能希望仅将前N
值计数较小,然后停止(例如,告诉用户它们的排名为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
You could get the count of number of entities that have total_value
less than or equal to the current users total_value
. 您可以获取total_value
小于或等于当前用户total_value
的实体数。
You could use the count() method to do so (on a Query object). 您可以使用count()方法(在Query对象上)执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.