When you import a batch of data inside a single transaction, one bad row is enough to roll back the whole batch. A savepoint is a marker inside a transaction that lets you roll back only the work done after that point, while keeping everything done before it. The simplest way to use one: nest a transaction.atomic() block inside an outer atomic() block.

The Problem

A familiar situation: importing a product list from a CSV file, all inside one transaction to guarantee “either everything is saved, or nothing is”. But input data is rarely perfectly clean — one row with a duplicate SKU, one row missing a required field. When the bad row hits the database, an IntegrityError fires and the whole transaction rolls back: 999 good rows are lost with it, even though the error was in a single row.

from django.db import transaction
def import_products(rows):
with transaction.atomic():
for row in rows:
Product.objects.create(
name=row["name"], price=row["price"]
)

A single duplicate key is enough to make the entire batch disappear, and then you have to run it all over again.

The Solution

Option 1: Nested atomic() — automatic savepoints

When a transaction.atomic() block sits inside another atomic() block, Django automatically creates a savepoint at the start of the inner block. If the inner block fails, Django rolls back exactly to that savepoint, while the outer transaction stays alive and keeps going.

from django.db import IntegrityError, transaction
def import_products(rows):
with transaction.atomic():
for row in rows:
try:
with transaction.atomic(): # automatic savepoint
Product.objects.create(
name=row["name"], price=row["price"]
)
except IntegrityError:
continue # only this row is skipped, the batch is saved

The result: good rows are saved, duplicate-SKU rows are skipped. Verified on Django 6.1.2: a batch of 3 rows with 1 duplicate SKU saved exactly 2 rows.

Option 2: Manual savepoint() when you need precise control

If you want to decide the rollback point yourself in code, use the three functions transaction.savepoint(), savepoint_rollback() and savepoint_commit().

from django.db import transaction
with transaction.atomic():
Product.objects.create(sku="B001", name="Loa", price=500000)
sid = transaction.savepoint()
Product.objects.create(sku="B002", name="Tai nghe", price=800000)
transaction.savepoint_rollback(sid) # only B002 disappears
Product.objects.create(sku="B003", name="Webcam", price=1200000)
# After commit: B001 and B003 remain, B002 has been rolled back

Note: savepoints only work when the database supports them — PostgreSQL, MySQL (InnoDB) and SQLite all do.

Apply It to Your Project

This pattern works right away for any batch job: importing CSV/Excel files, syncing data from an external API, seeding sample data, or processing a stream of webhooks. A practical rule: wrap each smallest unit of work (one row, one record, one event) in its own inner atomic() block, and log bad rows to a separate file to handle later. Only let truly serious errors — like losing the database connection — escape and roll back everything.

Related Posts

FAQ

How is a savepoint different from nested transaction.atomic()?

It is not — nested atomic() is exactly how Django creates savepoints automatically. The inner block opens a savepoint on entry and releases it on a clean exit; if the inner block fails, Django rolls back to that savepoint. Use manual savepoint() only when you need a rollback point that does not line up with the boundary of an atomic() block.

Do savepoints slow down imports?

Each savepoint costs one extra SQL statement, which is tiny compared to a single INSERT. For batches of a few thousand rows the difference is almost unmeasurable. Only when a batch reaches tens of thousands of rows is it worth measuring — and then you can group several rows under one savepoint instead of one savepoint per row.

What if an error in the inner block is not caught?

The exception escapes the outer atomic() block and rolls back the whole transaction as usual. Savepoints only help when you catch the error inside the inner block — which is why the sample code above always wraps the inner block in try/except IntegrityError.

When should I use atomic(savepoint=False)?

When the inner block is a mandatory step — without it the whole transaction is meaningless — and you want an error there to break the outer transaction too. Verified: after such an error the outer transaction is “broken” and every following query raises an error.

💬 Have you tried this in your own project? If you hit an error or have a shorter way to do it, leave a comment below — I read them all and will add the best ideas to the post.

Sources