使用F()在更新操作中反转布尔字段 [英] Invert boolean field in update operations with F()

查看:85
本文介绍了使用F()在更新操作中反转布尔字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 update 操作来还原 boolean 值。

I need, through an update operation, revert a boolean value.

我尝试过:

Item.objects.filter(serial__in=license_ids).update(renewable=not F('renewable'))

但是它不起作用。我确保字段未设置为 null

But it doesn't work. I made sure the fields are not set to null.

推荐答案

不这里不支持。您将必须使用何时情况

Not is not supported here. You will have to use Case When

from django.db.models import Case, Value, When

Item.objects.filter(serial__in=license_ids
).update(renewable=Case(
    When(renewable=True, then=Value(False)),
    default=Value(True))
    )
)

这篇关于使用F()在更新操作中反转布尔字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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