86π
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/stable/ref/signals/#django.db.models.signals.post_save
26π
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 wraps
def 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_loaddata
def callback_create_profile(sender, **kwargs):
# check if we are creating a new User
...
- [Django]-Django: TemplateDoesNotExist (rest_framework/api.html)
- [Django]-Django model manager objects.create where is the documentation?
- [Django]-What are the limitations of Django's ORM?
16π
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
- [Django]-Django β No such table: main.auth_user__old
- [Django]-Sort order of Django Admin records
- [Django]-Alowing 'fuzzy' translations in django pages?
3π
I faced a similar problem in one of my projects. In my case the signals were slowing down the tests as well. I ended up abandoning signals in favour of overriding a Model.save()
method instead.
In your case however I donβt think it makes sense to achieve this by overriding any save()
methods. In that case you might want to try this. Warning, I only tried it once. It seemed to work but is not thoroughly tested.
- Create your own test runner.
- Before you load the fixtures, disconnect the
callback_create_profile
function from theUser
classβpost_save
signal. - Let the fixtures load.
- Connect the function back to the signal.
- [Django]-How to migrate back from initial migration in Django 1.7?
- [Django]-Django β get() returned more than one topic
- [Django]-Django ModelForm: What is save(commit=False) used for?