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

前端 未结 2 1147
迷失自我
迷失自我 2020-12-08 21:19

I have a series of tests and cases in a database. Whenever a test is obsoleted, it gets end dated, and any sub-cases of that test should also be end dated. I see two ways

2条回答
  •  我在风中等你
    2020-12-08 21:25

    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.

提交回复
热议问题