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 ')
这个工作很好,但只有在该字段中实际有一个值。如果没有必要的日期,则值为无
。然后,工单列表中的所有无
,然后按顺序排列剩余的工单。
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.
如何获取底部的无
?
推荐答案
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屋!