简体   繁体   English

在Django中计算用户等级的最佳方法是什么

[英]What is the best way to calculate rank of users in Django

I have a web-site written in django. 我有一个用django编写的网站。 Site have users who have points and a rank according to their points. 网站上的用户具有得分和根据其得分的排名。

class Profile(UserenaBaseProfile):
    points = models.IntegerField(default=100)
    rank = models.PositiveIntegerField(...)

When a user gains a point his/her rank will be changed and so other users as well. 当用户获得积分时,他/她的排名将会改变,其他用户也是如此。 The question is what is the best way to calculate rank of the users and when i should calculate or how often? 问题是什么是计算用户排名的最佳方法?什么时候应该计算?

Thanks. 谢谢。

Probably not the best approach to update the rank of every user each time a single user's points increase. 每次增加一个用户的积分时,更新每个用户的排名的最佳方法可能都不是。 If you have a lot of users this would be harsh on the back end. 如果您有很多用户,那么后端将很苛刻。

If you want to display the users rank on say a profile page perhaps you could have a query that gets a list of all users ordered by points, store this separately as a dictionary or whatever and cache it heavily. 如果要在配置文件页面上显示用户排名,也许您可​​以进行查询以获取按点排序的所有用户的列表,将其分别存储为字典或其他内容,并大量缓存。 This will is better for performance. 这将对性能更好。

so lets say your users list looks like so(the 1111 etc being the usersid): 因此,可以说您的用户列表如下所示(1111等为用户ID):

USERS = { 1111: {'username': 'iskorum', 'points':99999 }, 2222: {'username': 'sidarcy', 'points':9444 }, 3333: {'username': 'joeblogs', 'points':37 } }

When it comes to actually getting the users rank you can simply find the users position in this stored dictionary. 当要真正获得用户排名时,您只需在此存储的词典中找到用户位置即可。

lets say the users id is 1111 假设用户ID为1111

userrank = [i+1 for i,x in enumerate(sorted(USERS)) if x == 1111] userrank = [如果x == 1111,则i,x中的i,x为i + 1(已排序(用户))]

returns 1 返回1

No extra DB query's needed 无需额外的数据库查询

If its only a small site no harm getting the user list each time and finding the current users position in that list. 如果只是一个很小的站点,那么每次获取用户列表并在该列表中查找当前用户位置均无害。

However if your expecting thousands of users the above may be a better approach. 但是,如果您期望成千上万的用户,则上述方法可能是更好的方法。

Edit: You should also cache the sorted list, as discussed in the comments below sorting the list each time could eat up a lot of memory 编辑:您还应该缓存已排序的列表,如下面注释中所讨论的,每次对列表进行排序可能会占用大量内存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Django实现对用户文件进行远程存储的最佳方法是什么 - what is the best way to achieve remote storage for users' files using django python / django中允许用户将文件添加到数据库的最佳方法是什么? - What is the best way in python/django to let users add files to the database? 基于不带名称的值筛选Django QuerySet的最佳方法(等级) - Best way to filter a django QuerySet based on value without name (rank) 在Django中处理多种类型的用户的最佳方法 - Best way to handle multiple type of users in Django Django:创建不同类型的用户并互换使用的最佳方法是什么? - Django: What's the best way to create different “kinds” of users and use them interchangeably? 查询与用户具有一对一关系的Django模型实例的最佳方法是什么 - What is the best way to query for instances of django models with one to one relationships with users 计算迭代操作百分比的最佳方法是什么? - What is the best way to calculate percentage of an iterating operation? 计算标准差的最佳方法是什么 - What is the best way to calculate the standard deviation 在熊猫数据框中计算回报的最佳方法是什么? - what is the best way to calculate the return in a pandas dataframe? 允许用户搜索项目的最佳方法是什么 - What is the best way to allow users to search for items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM