CONCAT to update db text field
Danny Crasto
1 min read
I needed to collect messages from different processes which was triggered by a record insertion. To keep things simple and denormalized, this stack overflow gem allows for that.
from django.db import models
class DBModel(models.Model):
@classmethod
def append_text(cls, event_id, text_field, message)
cls.objects.filter(id=event_id).update(
text_field=Concat(text_field, Value(message))
)
Using the database function CONCAT
, it offloads serializability to the database guaranteeing no race conditions.
Concat(ConcatPair(F(text_field), Value(message)))
Using F
methods, it simply appends message
to the existing field.
Happy hackin’!
Reference:
0
Subscribe to my newsletter
Read articles from Danny Crasto directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Danny Crasto
Danny Crasto
I am developer/code-reviewer/debugger/bug-fixer/architect/teacher/builder from dubai, uae