【问题标题】:Sort django query set based on orders in a list根据列表中的订单对 django 查询集进行排序
【发布时间】:2020-04-16 12:33:42
【问题描述】:

我有一个 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>}

【问题讨论】:

  • 怎么变成了[True, True, False],不应该是[True, False, False] 吗?
  • 你是对的,这是一个错字,我已经更正了。 @DavitTovmasyan

标签: python django sorting django-queryset django-orm


【解决方案1】:

这可能是一个解决方案:

[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)]

【讨论】:

  • 它擦除查询集中的数据@DavitTovmasyan
  • 实际上,在对压缩实体进行排序后,我需要 the_query_set 本身。 @DavitTovmasyan
猜你喜欢
  • 2016-06-08
  • 2018-01-23
  • 2015-10-09
  • 1970-01-01
  • 2018-02-09
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多