Skip to content

Conversation

@ticosax
Copy link

@ticosax ticosax commented Mar 13, 2025

fixes #135

Comment on lines +311 to +319
@contextmanager
def bypass_hooks_for(models: Iterable[T]):
try:
for model in models:
setattr(model, _MARKER, True)
yield
finally:
for model in models:
delattr(model, _MARKER)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tried it yet, but I'm not sure if this would work in every situation. What happens if you set that marker to a model, but what happens if that model is imported in another file? Would that work?

I mean something like this:

# utils.py
from .models import MyModel


def update_model_timestamp(pk: int):
    instance = MyModel.objects.get(pk=pk)
    instance.timestamp = now()
    instance.save()
# models.py
from utils import update_model_timestamp

def foo():
    with bypass_hooks_for((MyModel,)):
        update_model_timestamp(pk=1)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it's working also that way. I can testimony it's working in our project, running with cpython.
I'm fairly confident we can treat modules and their classes as singletons, the default import system is storing the modules in a global dict.
https://docs.python.org/3/faq/programming.html#how-do-i-share-global-variables-across-modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

skip_hooks context manager

2 participants