根据列表中的订单对 django 查询集进行排序
Sort django query set based on orders in a list
我有一个 Django 查询集:
the_query_set = {<obj1>, <obj2>, <obj3>}
和相应的列表:
the_list = [False, True, True]
如何按已排序列表的顺序对 the_query_set
进行排序:
the_sorted_list = [True, True, False]
desired_sorted_query_set = {<obj2>, <obj3>, <obj1>}
这可能是一个解决方案:
[obj for _, obj in sorted(zip(the_list, the_query_set), key=lambda group: group[0])]
或
[obj for _, obj in sorted(zip(the_list, the_query_set), key=lambda group: group[0], reverse=True)]
我有一个 Django 查询集:
the_query_set = {<obj1>, <obj2>, <obj3>}
和相应的列表:
the_list = [False, True, True]
如何按已排序列表的顺序对 the_query_set
进行排序:
the_sorted_list = [True, True, False]
desired_sorted_query_set = {<obj2>, <obj3>, <obj1>}
这可能是一个解决方案:
[obj for _, obj in sorted(zip(the_list, the_query_set), key=lambda group: group[0])]
或
[obj for _, obj in sorted(zip(the_list, the_query_set), key=lambda group: group[0], reverse=True)]