Have loaddata ignore or disable post_save signals

前端 未结 1 363
夕颜
夕颜 2021-02-05 11:15

Let\'s say that you want to setup a test environment for major changes to an application that you have created, and you want to make sure that those data existing in your system

1条回答
  •  时光说笑
    2021-02-05 11:40

    One way to achieve this is to add a decorator that looks for the raw keyword argument when the signal is dispatched to your receiver function. This has worked well for me on Django 1.4.3, I haven't tested it on 1.5 but it should still work.

    from functools import wraps
    def disable_for_loaddata(signal_handler):
        """
        Decorator that turns off signal handlers when loading fixture data.
        """
    
        @wraps(signal_handler)
        def wrapper(*args, **kwargs):
            if kwargs.get('raw'):
                return
            signal_handler(*args, **kwargs)
        return wrapper
    

    Then:

    @disable_for_loaddata
    def your_fun(**kwargs):
        ## stuff that won't happen if the signal is initiated by loaddata process
    

    Per the docs, the raw keyword is: True if the model is saved exactly as presented (i.e. when loading a fixture).

    0 讨论(0)
提交回复
热议问题