from django.db import transaction, DatabaseError
try:
    a.save()
except DatabaseError:
    transaction.rollback()
DatabaseError: current transaction is aborted, commands ignored until end of transaction block