Django按日期排序,但“无"最后? [英] Django Order By Date, but have "None" at end?
问题描述
我有一个工单模型,其中有一个字段用于显示何时需要工单.要获取工单列表以及早期需要的工单列表,我会这样做:
I have a model of work orders, with a field for when the work order is required by. To get a list of work orders, with those that are required early, I do this:
wo = Work_Order.objects.order_by('dateWORequired')
这很好用,但前提是该字段中确实有值.如果没有必需的日期,则值为 None
.然后,工单列表的顶部是所有None
,然后是其余工单以正确的顺序排列.
This works nicely, but ONLY if there is actually a value in that field. If there is no required date, then the value is None
. Then, the list of work orders has all the None
's at the top, and then the remaining work orders in proper order.
我怎样才能得到底部的None
?
How can I get the None
's at the bottom?
推荐答案
q = q.extra(select={
'date_is_null': 'dateWORequired IS NULL',
},
order_by=['date_is_null','dateWORequired'],
)
您可能需要在 order_by 部分中的 date_is_null 之前添加 -,但这就是您可以控制行为的方式.
You might need a - before the date_is_null in the order_by portion, but that's how you can control the behavior.
这篇关于Django按日期排序,但“无"最后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!