Chapter 13: Django Update Data

1. Quick Overview – The Main Ways to Update Data

Method What it does Speed (relative) Affects how many rows? Uses F()? Signals sent? Best for
instance.save() Update one object (full or partial) Slow (1 query) 1 Yes (via attr) Yes Forms, single record edits
instance.save(update_fields=…) Update only specified fields Slightly faster 1 Yes Yes Performance on large models
Model.objects.filter(…).update(…) Bulk SQL UPDATE – very efficient Fast (1 query) Many Yes (via F()) No Mass updates (e.g. approve all)
bulk_update(objs, fields=…) Update many pre-fetched objects in batches Medium-Fast Many No No When you already have objects in memory
update_or_create() Update if exists, create if not (single) Medium 1 No Yes Config / seed data
bulk_create(…, update_conflicts=…) Bulk upsert (since Django 4.1) Very fast Many Limited No Imports with possible duplicates

2. Method 1: Update One Object – The Classic .save()

Most common in views/forms.

In shell (python manage.py shell):

Python

Pro tip: Always use update_fields= when possible — especially on models with many/large fields (TextField, ImageField). Prevents unnecessary writes.

In a view example (e.g. edit question):

Python

3. Method 2: Atomic Increments – Use F() Expressions (Very Important for Votes!)

You never want to do choice.votes += 1; choice.save() in high-traffic apps — race conditions!

Instead use F() (expression that references current DB value):

Python

Full voting example (in your vote view):

Python

This runs pure SQL: UPDATE … SET votes = votes + 1 WHERE id = … — no race conditions!

4. Method 3: Bulk Update Many Rows – QuerySet.update()

Fastest for mass changes — one SQL statement.

Python

Warning: .update() bypasses .save() → no signals, no custom save logic, no auto_now fields updated.

5. Method 4: Bulk Update from Objects in Memory – bulk_update()

When you already fetched objects and modified them:

Python

Pros: Fewer queries than looping .save() Cons: Still needs objects in memory (not pure SQL like .update())

6. Method 5: Update or Create – update_or_create()

Single-row upsert:

Python

7. Advanced: Bulk Upsert with bulk_create(…, update_conflicts=…) (Django 4.1+)

For importing data that may already exist:

Python

Very powerful for CSV imports or sync jobs.

Common Mistakes & Fixes (Real Hyderabad Project Stories)

  • Race condition on votes → Forgot F() → use atomic update(F())
  • signals not firing → Used .update() or bulk_update when you needed post_save
  • updated nothing → Filter returned empty QuerySet → check .count() first
  • bulk_update fails → Forgot to specify fields= list
  • auto_now not updated → .update() / bulk_update skips them → use F(‘updated_at’) = timezone.now() if needed

Quick Exercise for You

  1. In shell: Create 3 choices for question #1
  2. Use F() to increment votes on one choice
  3. Use .update() to reset votes on all choices for that question
  4. Try bulk_update on a few modified objects

Tell me:

  • “Worked! Show me update in a real form view”
  • “How to update related objects (e.g. all choices when question changes)”
  • “Explain race conditions deeper with example”
  • “Error: Cannot update using F() — help!”
  • Or next: “Django Delete Data” or “Forms time!”

You’re handling data like a senior dev already — keep rocking it! 🚀🇮🇳

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *