40👍
Tested with django 1.9
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',
)
)
UPDATE Django 2.1
https://docs.djangoproject.com/en/2.2/ref/models/database-functions/#replace
from django.db.models import Value
from django.db.models.functions import Replace
ExampleModel.objects.filter(<condition>).update(
string_field=Replace('string_field', Value('old text'), Value('new text'))
)
5👍
You could make your own F
-like object to represent the string replacing in SQL. Here is a proof of concept:
from django.db.models.expressions import ExpressionNode
class StringReplaceF(ExpressionNode):
def __init__(self, field, replace_from, replace_to):
self.field = field
self.replace_from = replace_from
self.replace_to = replace_to
super(StringReplaceF, self).__init__()
def evaluate(self, evaluator, qn, connection):
return (
"REPLACE({}, %s, %s)".format(self.field),
(self.replace_from, self.replace_to)
)
>>> f = StringReplaceF('string_field', 'old text', 'new text')
>>> ExampleModel.objects.update(string_field=f)
You’d need to do a bit more work with the class if you need it to behave nicely with other F
objects, but then again, the existing F
objects don’t seem to work with strings anyway.
- [Django]-Which Model Field to use in Django to store longitude and latitude values?
- [Django]-(fields.E300) Field defines a relation with model which is either not installed, or is abstract
- [Django]-How can i pass optional arguments in Django view
3👍
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'))
)
- [Django]-Django FileField upload is not working for me
- [Django]-Django: Display Choice Value
- [Django]-Django TypeError: render() got an unexpected keyword argument 'renderer'
0👍
Use replace_field_substring(YourModel, "your_field", "value_from", "value_to")
from django.db.models import F, Value, QuerySet
from django.db.models.functions import Replace
def replace_field_substring(model_or_qs, field, value_from, value_to):
"""Generates ORM partial replace, which can be used in Django ORM data migrations,
for example for partial replace tbl.cont from="twitter.com" to="x.com" will give you
UPDATE tbl SET cont = REPLACE(cont, 'twitter.com', 'x.com') WHERE cont LIKE '%twitter.com%';
"""
qs = model_or_qs if isinstance(model_or_qs, QuerySet) else model_or_qs.objects.all()
replacement_expression = Replace(F(field), Value(value_from), Value(value_to))
qs.update(**{field: replacement_expression})
- [Django]-ForeignKey to abstract class (generic relations)
- [Django]-How to use refresh token to obtain new access token on django-oauth-toolkit?
- [Django]-What's the best solution for OpenID with Django?
-1👍
Django 2.2 support bulk update, can you using function that one.
Check this one : https://docs.djangoproject.com/en/2.2/ref/models/querysets/#django.db.models.query.QuerySet.bulk_create
- [Django]-Django & Redis: How do I properly use connection pooling?
- [Django]-How to define two fields "unique" as couple
- [Django]-Backwards migration with Django South