Opened 6 years ago

Last modified 6 years ago

#29557 closed New feature

add on_commit decorator — at Initial Version

Reported by: obayemi Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: transaction on_commit decorator
Cc: Carlton Gibson Triage Stage: Unreviewed
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

I recently had to add on_commit hooks with lambdas to some functions (mainly email notifications in post_save signals handler)
That was a lot of boilerplate so I wrote a simple decorator for making a function call only trigger on transaction commit

def call_on_commit(f):
    """
    only call the decorated function at transaction commit
    warning the return value will be ignored
    """
    def handle(*args, **kwargs):
        transaction.on_commit(lambda: f(*args, **kwargs))
    return handle

leading to

@call_on_commit
def send_message(user, context):
    # send message

instead of

def send_message(user, context):
    def _send_message():
        # send message
    on_commit(do_stuff)

I made a PR on github to implement this feature
https://github.com/django/django/pull/10167

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top