如何在乌龟 orm 中使用 CASE WHEN THEN ELSE

问题描述

嗨,我正在尝试在 tortoise-orm 中使用 CASE WHEN THEN ELSE。 我尝试了以下但没有奏效:

import datetime
import tortoise.models
import tortoise.fields
import tortoise.expressions
from pypika import Case

class Task(tortoise.models.Model):
    uid = tortoise.fields.UUIDField(pk=True)
    name = tortoise.fields.CharField(255)
    name_updated_at = tortoise.fields.data.DatetimeField(auto_Now_add=True,null=False)

await Task.filter(uid="my-super-uid").update(
    name=Case()
    .when(tortoise.expressions.F('name_updated_at') > datetime.datetime.utcNow(),"Hi")
    .else_(tortoise.expressions.F('name'))
)

我知道 CASE 是 supported by pypika,但我不知道如何在 tortoise-orm 中使用它。 此外,我确实想使用 CASE,因为我想在一次更新中为多个字段使用案例,而不是使用不同的 filter(...) 参数进行多次更新。

知道如何使用 CASE()??

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)