Django bulk update with string replace

前端 未结 4 638
Happy的楠姐
Happy的楠姐 2020-12-28 16:27

I am trying to update and modify a string field Django\'s ORM. The equivalent SQL to do this is:

UPDATE example_table SET string_field = REPLACE(string_fiel         


        
4条回答
  •  长发绾君心
    2020-12-28 17:17

    New in Django 2.1 - Replace database function

    Your example can now be expressed most easily via:

    ExampleModel.objects.update(string_field=Replace('string_field', Value('old_text'), Value('new_text')))
    

提交回复
热议问题