Why does Django bulk_create
return objects without pk
's?
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
In the result objs == item_list
What sense is there in this?
I mean, this method could return a result of the operation (i.e. True
, or False
, or something else), not this useless collection of objects that I already have.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With