Is the models' object.objects.bulk_update() and bulk_create() function supported? #16972
Replies: 3 comments
-
We do see bulk_update is use in the code base - few examples:
And bulk_create is used in the testing files - few examples:
|
Beta Was this translation helpful? Give feedback.
-
It can be. Example: I wrote a custom script which used bulk_create() to add missing components to devices (from the device type). However, because the hooks aren't called, it didn't update the cached counters of the number of each type of component on a device, which meant that the tabs in the GUI either showed the wrong count, or the tabs were missing completely (if the count == 0) Actually the pull request which fixes that hasn't been merged. It changes:
to:
|
Beta Was this translation helpful? Give feedback.
-
That what I was thinking as I was reading the source code more. It seems like it might an unsafe operation, might have to submit a feature request to add support for update() in models that support custom save(). Guessing we will have to go model-by-model to see what one might be safe and what might not be...... or just live with 60+ minute run time for some of our deployment scripts. 😄 |
Beta Was this translation helpful? Give feedback.
-
We have a few scripts that do large bulk operations like synchronizing a hundred VLANs to interfaces with tags or pull in IPAM data from external sources. Today we use the
object.save()
takes some time to run (like .3 sec per vlan per interface – one run took 60+ minutes). We wanted to start using the functionbulk_update()
to help speedup this operation based on some limited testing in our development environment. Reading the docs for bulk_update theupdate()
function is called and thepre_save
andpost_save
signals aren’t sent. Will this be an issue if we convert our scripts to use bulk_update vs a loop of saves?https://docs.djangoproject.com/en/5.0/ref/models/querysets/#bulk-update
https://docs.djangoproject.com/en/5.0/ref/models/querysets/#bulk-create
Beta Was this translation helpful? Give feedback.
All reactions