【问题标题】:How to return default value if field is NULL?如果字段为NULL,如何返回默认值?
【发布时间】:2013-12-08 09:28:22
【问题描述】:

我需要类似 sql IS_NULL 函数。比如在postgres中,我可以这样写

coalesce(field, 'Empty')

如何使用 Django 实现这一点?

【问题讨论】:

标签: python django orm


【解决方案1】:

您可以使用extra查询集方法:

MyModel.objects.extra(select={'field': "coalesce(field, 'Empty')"})

COALESCE 在大多数数据库引擎中的工作方式应该类似。

【讨论】:

  • 它将我的浮点字段更改为 u'unicode'。我想要浮动作为回报。怎么做?
猜你喜欢
  • 1970-01-01
  • 2013-05-12
  • 2018-10-22
  • 2019-04-12
  • 1970-01-01
  • 2015-09-01
  • 2016-11-10
  • 1970-01-01
  • 2012-08-19
相关资源
最近更新 更多