Skip to content

Commit 0de6c0a

Browse files
committed
use functools.partial to enforce instance scope
1 parent 322663d commit 0de6c0a

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

django_lifecycle/mixins.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from functools import reduce, lru_cache
1+
from functools import partial, reduce, lru_cache
22
from inspect import isfunction
33
from typing import Any, List
44

@@ -238,9 +238,10 @@ def _run_hooked_methods(self, hook: str, **kwargs) -> List[str]:
238238
# the same hook within the atomic transaction and on_commit
239239
method_name = method_name + "_on_commit"
240240

241-
def _on_commit_func():
242-
method(self)
243-
241+
# Use partial to create a function closure that binds `self`
242+
# to ensure its available to execute later.
243+
_on_commit_func = partial(method, self)
244+
_on_commit_func.__name__ = method_name
244245
transaction.on_commit(_on_commit_func)
245246
else:
246247
method(self)

0 commit comments

Comments
 (0)