Home > database >  How do I get created objects as a query set from django bulk_create?
How do I get created objects as a query set from django bulk_create?

Time:07-25

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.

CodePudding user response:

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 Entrys with the primary key filled in.

CodePudding user response:

When you send a post request after saving in database in the response you could see the data you need

  • Related