我在 Django 中注释查询集,如下所示:
q_set = q_set.annotate(
period=Concat(
ExtractYear(date_field),
Value(' - '),
ExtractMonth(date_field),
output_field=CharField()
),
)
但是我得到了这个结果:
2022 - 12
2023 - 1
2023 - 10
2023 - 11
2023 - 12
2023 - 2
2023 - 3
2023 - 4
2023 - 5
2023 - 6
2023 - 7
2023 - 8
2023 - 9
但是我需要按正确的顺序对其进行排序...
q_set = q_set.annotate(
period=Concat(
ExtractYear(date_field),
Value(' - '),
Right(Concat(Value('0'), ExtractMonth(date_field), output_field=CharField()), 2),
output_field=CharField()
),
)
2022 - 12
2023 - 01
2023 - 02
2023 - 03
2023 - 04
2023 - 05
2023 - 06
2023 - 07
2023 - 08
2023 - 09
2023 - 10
2023 - 11
2023 - 12