gpt4 book ai didi

django - 将 postgres 中列的值更新为小写

转载 作者:行者123 更新时间:2023-11-29 14:18:00 25 4
gpt4 key购买 nike

我有一个名为 service_table 的表,其中包含一些字段。一个字段有日期名称,如 SundayMondayTuesday 等。我想将 postgres 数据库表中的所有列值更改为小写.例如“星期日”更新为“星期日”我写了一个查询

update service_table set days=lower(days);

但它显示

Hint: No function matches the given name and argument types. You might need to add explicit type casts.

注意:这个表有一些外键。

最佳答案

您可以在 Django shell 中执行此操作

from django.db.models import F
from django.db.models.functions import Lower

service_table.objects.update(days=Lower(F('days'))

Django update() 是直接更新的批量操作。

Django F() 可以引用模型字段值并使用它们执行数据库操作,而无需实际将它们从数据库中拉出到 Python 内存中。

F() 帮助拉取模型字段值,然后根据需求处理并使用 update() 将其保存回来

关于django - 将 postgres 中列的值更新为小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41296834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com