title: Django bulk update with string replace
body: <p>I am trying to update and modify a string field Django's ORM.  The equivalent SQL to do this is:</p>
  
  <pre><code>UPDATE example_table SET string_field = REPLACE(string_field, 'old text', 'new text');
  </code></pre>
  
  <p>With that query, I expect <code>old text</code> and <code>old text more text</code> to be replaced with <code>new text</code> and <code>new text more text</code> respectively, for all the entries in the <code>string_field</code> column.</p>
  
  <p><a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#update" rel="noreferrer">Bulk update()</a> seems promising, but doesn't allow me to modify only part of the field, and <a href="https://docs.djangoproject.com/en/dev/ref/models/expressions/#f-expressions" rel="noreferrer">F() expressions</a> only implement numeric changes, not string replace.  I also looked at using <a href="https://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly" rel="noreferrer">raw queries</a> to run the above SQL, but that seems like a sideways hack (especially since F() exists to do the same functionality on numbers), and I couldn't get them to actually execute.</p>
  
  <p>I ended up with this, but it seems a shame to execute all the extra queries when I know there's a one line SQL statement to do it.</p>
  
  <pre><code>for entry in ExampleModel.objects.all():
      entry.string_field = entry.string_field.replace('old text', 'new text', 1)
      entry.save()
  </code></pre>
  
  <p>Does this feature not exist in Django's ORM for strings yet? Is there something I overlooked in the docs?</p>
  
  <p>Related SO questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/15319875/generate-update-query-using-django-orm">generate update query using django orm</a></li>
  <li><a href="https://stackoverflow.com/questions/12030143/sql-statement-to-remove-part-of-a-string">SQL statement to remove part of a string</a></li>
  </ul>