我有一個長度 = 10000 個用戶的查詢集
sorted_users = User.ojbects.all().order_by("-money")
我需要在這個排序的查詢集中獲得特定用戶的位置,
我可以用 for 回圈做到這一點:
user_position = 1
for account in sorted_users:
if account.username == "admin":
break
my_position = 1
print(user_position)
它作業正常,但如果用戶數量增長到 100 000,每次計算用戶位置都需要很長時間,for loop速度很慢
我怎樣才能讓它更快,沒有for loop?
uj5u.com熱心網友回復:
您可以嘗試獲取用戶 ID 串列,然后檢查您的管理員用戶的索引:
users_ids = list(User.objects.all().order_by("-money").values_list('id', flat=True))
admin_index = users_ids.index(<admin_id>)
print(admin_index)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/487013.html
標籤:django
