[Django]-Django: When to customize save vs using post-save signal

30👍

I generally use this rule of thumb:

  • If you have to modify data so that the save won’t fail, then override save() (you don’t really have another option). For example, in an app I’m working on, I have a model with a text field that has a list of choices. This interfaces with old code, and replaces an older model that had a similar text field, but with a different list of choices. The old code sometimes passes my model a choice from the older model, but there’s a 1:1 mapping between choices, so in such a case I can modify the choice to the new one. Makes sense to do this in save().
  • Otherwise, if the save can proceed without intervention, I generally use a post-save signal.
👤mipadi

16👍

In my understanding, signals are a means for decoupling modules. Since your task seems to happen in only one module I’d customize save.

👤jammon

Leave a comment