[Django]-Why does Django bulk_create return objects without pk?

20👍

Quoting from the django doc:

If the model’s primary key is an AutoField it does not retrieve and
set the primary key attribute, as save() does.

According to django, it creates a list of database records in one shot, but the objects’ ids are not retrieved. I think it’s good for the situation where you do large insertions without further processing the data.

10👍

As of Django 1.10 docs:

If the model’s primary key is an AutoField it does not retrieve and set the primary key attribute, as save() does, unless the database backend supports it (currently PostgreSQL).

So if you are using Django 1.10+, and you are not getting primary keys returned, it’s because only PostgreSQL supports it.

Leave a comment