Skip to content

Django Model Lifecycle Hooks – Inspired by Rails Callbacks

github.com/rsinger86
2 pointsrsinger873 comments
On HN

Comments

Very cool!

At my current place of employment, I have been trying to push Django signals for a little while now as a good way to drive our metrics.

One of the chief concerns about signals however, is that they're fairly isolated, and it's not always obvious what gets triggered when (for example) save() is called. This is, of course, one of the drawbacks to loose coupling.

Something like @hook would be a good response to those who have this concern, because the hook code is attached to the model directly, meaning when you wonder to yourself, "What happens when I call save()?", you need look no further than the model itself.

I'll pitch this to my colleagues and see what they have to say!

Cool :)

Yeah, my team hasn't found many cases where signals are preferable to overridding save(). The implementation of @hook is really just a wrapper around that approach - with the aim of a friendly, declarative interface.

Hey all -

Just wanted to share a little package I made to add lifecycle event hooks to Django models. I often find this style of hooking into events easier to reason about than Django's built-in Signals.

Feedback is welcomed and appreciated.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.