[Fixed]-How do you rollback during Django shell session after causing DatabaseError?

20👍

This happens to me all the time when using Postgres, and it’s really irritating.

You want:

from django.db import transaction
transaction.rollback()

Most of the time this is fine (and in my experience, it’s safe to ignore the TransactionManagementError).

👤Seth

7👍

This is slightly better because you get no stack trace about TransactionManagementError:

from django.db import transaction
transaction.rollback_unless_managed()

Leave a comment