How to save a model without sending a signal?

前端 未结 6 2118
名媛妹妹
名媛妹妹 2021-02-06 23:23

How can I save a model, such that signals arent sent. (post_save and pre_save)

6条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-07 00:12

    If you have mutual relations on models and their signals still you can decouple signal's logic to have more signals of same type, and handle your logic in more sophisticated way:

    You can check in signals, the state of object:

    kwargs['created']

    You can check the state of any pasted extra value: So in one signal, you will read at first:

    if `kwargs['instance'].skip_signals`:
       return
    

    and in another place, before save() you will just set that skip_signals on the specific object, in specific situation. (there is no need to define it as model field)

    You can also do not emit signals:

    • by overriding method(s) on models,
    • or by adding own save_without_signals(),
    • or just as mentioned already, doing filter(pk=<>).update(...)

提交回复
热议问题