Reputation: 2210
I just wonder how is transaction managed in django's admin commands. Commit on save? Commit on success? I can't find related info from official documents.
Upvotes: 8
Views: 3326
Reputation: 15289
Management command operations are not wrapped in transactions unless you tell them to.
You can tell the handle() method to be wrapped in a transaction by setting the output_transaction attribute to True. From the docs:
BaseCommand.output_transaction
A boolean indicating whether the command outputs SQL statements; if True, the output will automatically be wrapped with BEGIN; and COMMIT;. Default value is False.
For more control you can always initiate transactions yourself:
...
def handle(self, *args, **options):
with transaction.atomic():
do_your_stuff()
Upvotes: 5
Reputation: 7032
I'm not too sure, but admin forms don't reach to the commit point unless they meet clean() requirements. After that I guess everything will be committed. This behavior should be sufficient for the default forms in admin. However, for more complex forms, you can create your custom admin form and I'm pretty sure you can define whether or not you want to commit on success or on save.
Upvotes: -1