On 14/04/10 23:15, Bill Freeman wrote:
The trouble is, I don't know how. Just calling django.db.transaction.rollback()
doesn't work, and neither do several other guesses.
N.B. Despite eventually signalling a TransactionManagementError if
django doesn't "think" it's in a transaction-managed chunk, that method
typically _does_ successfully roll back the current db transaction
(ultimately with the connection._rollback() private method already
mentioned by Joe) prior to signalling the error. Which is a bit
confusing, yes.
Perhaps it would be nice if the interactive shell had an option to start
up already in the transaction managed mode*, but in the meantime you can
manage it yourself -
"just" interactively pretend to be some of the transaction management
setup code at the shell prompt, and you can avoid that error being
raised upon rollback() - i.e. before and after your stuff, type in what
@commit_manually does before and after the functions it decorates to
switch into and out of managed mode:
See source, but basically just
transaction.enter_transaction_management() then
transaction.managed(True) before and
transaction.leave_transaction_management() after - the latter will
rollback and raise an error if you forgot to commit or rollback, and
remember to commit and rollback as necessary during your stuff.
This is useful to know how to do anyway for complex interactive
manipulations from the shell you might want to rollback as a whole.
[Loosely related - there is also the autocommit django setting for
postgresql to consider, I haven't really 100% got my head around all
ramifications of it yet - this blog post has been very helpful though:
http://thebuild.com/blog/2009/11/07/django-postgresql-and-autocommit/
http://docs.djangoproject.com/en/dev/ref/databases/#autocommit-mode
- note in particular turning it on apparently doesn't preclude sections
of code being under full transaction management (as the name might
suggest to some people)]
* say:
current behaviour (which is same as @autocommit),
line-by-line commit on successful line or rollback if the line raises an
error, a session commit on successful exit, (two conceivably variations
analogous to @commit_on_success)
full manual management (analogous to @commit_manually)
--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.