亚洲色蝴蝶中文娱乐网,在线亚洲欧美一区二区中文字幕,无人视频在线观看视频高清视频,99午夜国产精品一区二区,人人妻人人爽人人狠狠

使用字符串替換的Django批量更新replace方法的使用

時間:2022-01-12 10:33:57 類型:python
字號:    
#方法一:
for entry in ExampleModel.objects.all():
    entry.string_field = entry.string_field.replace('old text', 'new text', 1)
    entry.save()

#方法二:
from django.db.models import F, Func, Value

ExampleModel.objects.filter(<condition>).update(
    string_field=Func(        F('string_field'),        Value('old text'), Value('new text'),
        function='replace',
    )
)

#方法三:

from django.db.models import Value
from django.db.models.functions import Replace

ExampleModel.objects.filter(<condition>).update(
    string_field=Replace('name', Value('old text'), Value('new text'))
)

#方法四:
#Django直接調(diào)用原生方法


<