Bulk Update for Performance

A snippet showing how to use bulk_update for faster updates.


# views.py

from .models import Event

def publish_events(request):
    events = list(Event.objects.filter(published=False)[:500])
    for e in events:
        e.published = True
    Event.objects.bulk_update(events, ["published"])
    return HttpResponse("Updated events.")
      
Explanation:
  • Updates many rows efficiently with a single bulk update call.
  • Greatly improves performance when you need to update many rows.
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.