expressions.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:django-postgres-extra 作者: SectorLabs 项目源码 文件源码
def IsNotNone(*fields, default=None):
    """Selects whichever field is not None, in the specified order.

    Arguments:
        fields:
            The fields to attempt to get a value from,
            in order.

        default:
            The value to return in case all values are None.

    Returns:
        A Case-When expression that tries each field and
        returns the specified default value when all of
        them are None.
    """

    when_clauses = [
        expressions.When(
            ~expressions.Q(**{field: None}),
            then=expressions.F(field)
        )
        for field in reversed(fields)
    ]

    return expressions.Case(
        *when_clauses,
        default=expressions.Value(default),
        output_field=CharField()
    )
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号