Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is transaction managed in Django's admin commands?

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.

like image 279
Georgie Porgie Avatar asked Oct 10 '22 09:10

Georgie Porgie


1 Answers

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()
like image 192
andyhasit Avatar answered Oct 20 '22 04:10

andyhasit