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:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With