rsinger86/django-lifecycle

Adding hooks to 3rd-party objects

Closed this issue · 2 comments

Our code provides some django-models and we use django-lifecycle. Our models are also used in a library-like way where other django-projects want to provide some post-creation workflows after our code creates those model objects.

How can we add a hook to a model without monkey patching it? The other code can't subclass our model because then our code won't be using their subclassed type.

It would be great if there was a way to register a hook onto a model in a way other than class definition somehow. If you have any pointers on how to add this, I wouldn't mind implementing it.

Assuming the model in question is already subclassing the LifecycleMixin / LifecycleModel class, maybe you could do something like:

Model.x = hook(BEFORE_CREATE)(Model.x)

But, that is basically "monkey patching". I don't think there would be any other way though, there is no global registry or anything like that being utilized.

Also, it's still a hard requirement that the model subclasses LifecycleModel / LifecycleMixin.

I don't know how this could be done, because as @dralley says, it's required to subclass LifecycleMixin.

If someone has some clues on how could this problem be solved, we're happy to accept a PR or even implement it ourselves, but until that I'm going to close this as won't fix.