One of my API calls can result in updates to a large number of objects (Django models). I'm running into performance issues with this since I'm updating each item individually, saving, and moving on to the next:
for item in Something.objects.filter(x='y'):
item.a="something"
item.save()
Sometimes my filter criterion looks like "where x in ('a','b','c',...)".
It seems the official answer to this is "won't fix". I'm wondering what strategies people are using to improve performance in these scenarios.
The ticket you linked to is for bulk creation - if you're not relying on an overridden save
method or pre/post save signals to do bits of work on save, QuerySet
has an update
method which you can use to perform an UPDATE
on the filtered rows:
Something.objects.filter(x__in=['a', 'b', 'c']).update(a='something')
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