Reputation: 11334
I use Django and I want update many records of my model by using a calculation like in SQL like that :
UPDATE table SET amount = pre_tax * 1.2 WHERE amount IS NULL;
I want do that with Django ORM. I didn't find any ways to do that.
This answer Django - Update model field based on another field don't use bulk update, I need a syntax that allows updating several records.
Upvotes: 1
Views: 1930
Reputation: 32294
You can use an F
expression and .update()
to update multiple records at once. The F
expression allows you to reference an existing value in the row
Model.objects.filter(amount__isnull=True).update(amount=F('pre_tax') * 1.2)
Upvotes: 5