I am making use of bulk_create on a Django model. How can I get the values of the created object so I can proceed to make use of it, without querying the db again?
Asked
Active
Viewed 85 times
1
-
Which database are you using? – Eduardo Alejandro Leyva Jul 24 '22 at 20:58
-
Please provide enough code so others can better understand or reproduce the problem. – Community Jul 24 '22 at 21:06
-
@EduardoAlejandroLeyva, i use postgres – Pascal Ezeama Jul 27 '22 at 19:22
2 Answers
1
The .bulk_create(…)
method [Django-doc] returns a list of the created items. Indeed:
This method inserts the provided list of objects into the database in an efficient manner (generally only 1 query, no matter how many objects there are), and returns created objects as a list, in the same order as provided.
So you can work, as the documentation states with:
objs = Entry.objects.bulk_create([ Entry(headline='This is a test'), Entry(headline='This is only a test'), ])
where objs
will be a list of two Entry
s with the primary key filled in.

Willem Van Onsem
- 443,496
- 30
- 428
- 555
-1
When you send a post request after saving in database in the response you could see the data you need

Abdulrhman bakr
- 9
- 1
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 25 '22 at 12:25