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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With