Django按日期排序,但具有“无”在结束? [英] Django Order By Date, but have "None" at end?

查看:124
本文介绍了Django按日期排序,但具有“无”在结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工单的模型,一个字段用于当工单要求时。要获得工作单的列表,请尽早使用这些工作单,我可以这样做:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆