Settings

Theme

Django Model Lifecycle Hooks – Inspired by Rails Callbacks

github.com

2 points by rsinger87 8 years ago · 3 comments

Reader

diminoten 8 years ago

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!

  • rsinger87OP 8 years ago

    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.

rsinger87OP 8 years ago

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.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection