[Django]-Have loaddata ignore or disable post_save signals

49👍

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).

Leave a comment