If you want to set the same value in a row collection , you can use the update () method in conjunction with any query term to update all rows in a single query:
some_list = ModelClass.objects.filter(some condition).values('id') ModelClass.objects.filter(pk__in=some_list).update(foo=bar)
If you want to update a collection of strings with different values depending on some conditions, you can at best package updates according to the values. Suppose you have 1000 rows in which you want to set one of the X values for a column, then you can prepare packages in advance, and then only execute X update requests (each essentially has the form of the first example above) + initial SELECT -query .
If each row requires a unique value, there is no way to avoid a single update request. Perhaps look at other architectures, such as CQRS / Event Sourcing, if you need performance in this latter case.
Andreas Bergström Nov 15 '18 at 14:05 2018-11-15 14:05
source share