Have loaddata ignore or disable post_save signals
django, django-signals
Solution
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).
Problem
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 will easily load into the new system. Django provides command line facilities for exporting and loading data. Via `dumpdata` and `loaddata` ``` python manage.py dumpdata app.Model > Model.json python manage.py loaddata Model.json ``` The documentation, identifies (although not explicitly) that some signals are ignored during this process: When fixture files are processed, the data is saved to the database as is. Model defined save methods and pre_save signals are not called. (source) Is there a way to disable `post_save` signal calls during the `loaddata` process? Possibly Related: - How do I prevent fixtures from conflicting with django post_save signal code? - https://code.djangoproject.com/ticket/8399