【发布时间】:2016-11-30 19:46:34
【问题描述】:
我有一个来自 redis 的排序集。排序后的集合包含对象的 id 及其排序分数。
接下来,我有一个 Django 查询集,它由排序后的 id 包含在上述 redis 排序集中的对象组成。
我需要根据 id 在 redis 排序集中的位置 对这个 Django 查询集进行排序。最快的方法是什么?
我正在尝试以下方法:
dictionary = dict(sorted_set) #turning the sorted set into a dictionary
for pk, score in dictionary:
obj = queryset.get(id=pk) #get object with id equalling pk
score = object #assign the object to the 'score' value of this key-value
result = dictionary.values() #making a list of all values, that are now in sorted order
上面的 for 循环因too many values to unpack 错误而失败 - 我仍在调试它。尽管如此,我也觉得可能有更快的方法来做我想做的事情,因此我问了这个问题。提前致谢!
【问题讨论】:
标签: python django sorting redis