Categories
Python Answers

How to do bulk update with Python Django?

Spread the love

To do bulk update with Python Django, we can use the bulk_update method.

For instance, we write

objs = [
   Entry.objects.create(headline='Entry 1'),
   Entry.objects.create(headline='Entry 2'),
]
objs[0].headline = 'This is entry 1'
objs[1].headline = 'This is entry 2'
Entry.objects.bulk_update(objs, ['headline'])

to create 2 Entry objects and put them on the objs list.

And then we call Entry.objects.bulk_update with objs and ['headline'] to update the headline column value of each Entry entry.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *