How do I prevent fixtures from conflicting with django post_save signal code?
I think I figured out a way to do this. There is a 'raw' parameter in the kwargs passed in along with signals so I can replace my test above with this one:
if (kwargs.get('created', True) and not kwargs.get('raw', False)):
Raw is used when loaddata is running. This seems to do the trick.
It is mentioned here: http://code.djangoproject.com/ticket/13299
Would be nice if this was documented: http://docs.djangoproject.com/en/1.2/ref/signals/#django.db.models.signals.post_save
This is an old question, but the solution I've found most straightforward is to use the 'raw' argument, passed by load data, and decorate the listener functions, for example:
from functools import wrapsdef disable_for_loaddata(signal_handler): @wraps(signal_handler) def wrapper(*args, **kwargs): if kwargs['raw']: print "Skipping signal for %s %s" % (args, kwargs) return signal_handler(*args, **kwargs) return wrapper
and then
@disable_for_loaddatadef callback_create_profile(sender, **kwargs): # check if we are creating a new User ...
Simple solution, add this to the beginning of your post_save function:
if kwargs.get('raw', False): return False
This will cause this function to exit when loading a fixture.
See: https://docs.djangoproject.com/en/dev/ref/signals/#post-save