Why django bulk_create returns objects without pk?

Why django bulk_create returns objects without pk?

In [1]: item_list = [Model(title=str(i)) for i in range(10)] In [2]: objs = Model.objects.bulk_create(item_list) In [3]: print(objs[0].pk) None 

As a result, objs == item_list

What is the point of this?

I mean, this method can return the result of the operation (True or False or someting else), and not this useless set of objects that I already have.

+5
source share
1 answer

Quote from django doc :

If the primary key of the model is AutoField, it does not extract and set the primary key attribute, as save () does.

According to django, it creates a list of database records in a single snapshot, but object identifiers are not retrieved. I think this is good for a situation where you make large inserts without further data processing.

+7
source

Source: https://habr.com/ru/post/1235709/


All Articles