How do you rollback during Django shell session after causing DatabaseError?
I used to know how to do this, but I forgot. Sometimes, while using the Django shell, you make a mistake and cause the transaction to become aborted. After that, any further queries you do will result in DatabaseError: current transaction is aborted, commands ignored until end of transaction block. However, I think there is a way to rollback the transaction when this happens manually so you don't have to restart your session. Can anyone tell me what it is?
This bug repor开发者_如何学编程t (https://code.djangoproject.com/ticket/10813) makes reference to the technique but doesn't explain it. Running django.db.transaction.rollback results in "TransactionManagementError: This code isn't under transaction management".
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
).
This is slightly better because you get no stack trace about TransactionManagementError
:
from django.db import transaction
transaction.rollback_unless_managed()
精彩评论