How to get primary keys of objects created using django bulk_create

2016

Since Django 1.10 – it’s now supported (on Postgres only) here is a link to the doc.

>>> list_of_objects = Entry.objects.bulk_create([
...     Entry(headline="Django 2.0 Released"),
...     Entry(headline="Django 2.1 Announced"),
...     Entry(headline="Breaking: Django is awesome")
... ])
>>> list_of_objects[0].id
1

From the change log:

Changed in Django 1.10:
Support for setting primary keys on objects created using bulk_create() when using PostgreSQL was added

Leave a Comment

techhipbettruvabetnorabahisbahis forumueduseduseduedueduedusedusedusedusedu