django bulk update

Solutions on MaxInterview for django bulk update by the best coders in the world

showing results for - "django bulk update"
Hajar
09 Aug 2019
1objs = []
2for person in p:
3    obj = People.objects.get(email=person['email'])
4    obj.birthday = person['birthday']
5    objs.append(obj)
6People.objects.bulk_update(objs, ['birthday'], batch_size=1000)
Luisa
17 Mar 2016
1objs = [
2    Entry.objects.create(headline='Entry 1'),
3    Entry.objects.create(headline='Entry 2'),
4]
5objs[0].headline = 'This is entry 1'
6objs[1].headline = 'This is entry 2'
7Entry.objects.bulk_update(objs, ['headline'])
8
9# Caveats #
10# -You cannot update the model’s primary key.
11#
12# -Each model’s save() method isn’t called, and the pre_save and post_save signals aren’t 
13# sent.
14#
15# -If updating a large number of columns in a large number of rows, the SQL 
16# generated can be very large. Avoid this by specifying a suitable batch_size.
17#
18# - Updating fields defined on multi-table inheritance ancestors will incur an extra query per ancestor.
19# If objs contains duplicates, only the first one is updated.
20