About •  Books •  Apps •  Jobs •   Feeds •  Twitter
Login

Turning off django signals

While I'm on the subject, how to turn off signals that you don't want on.

The first and most obvious signal to turn off is the one asking you for a username and password on syncdb, gosh that's annoying. You can do this by calling: 

python manage.py syncdb --noinput

But if you want to do this in Python you can:

from django.contrib.auth import models as auth_app
from django.contrib.auth.management import create_superuser
from django.db.models.signals import post_syncdb
post_syncdb.disconnect(create_superuser, 
       sender=auth_app, 
       dispatch_uid = "django.contrib.auth.management.create_superuser")

The part that was annoying was figuring out just how to get it to disconnect, to find that dispatch_uid I had to go and read the source, not onerous at all. My previous post was about signals vs save. To all that I'll add an annoyance with signals: if you load in fixtures the, signals get called. This is annoying because I wanted to do the following:

Unfortunately all the profiles failed, because the profiles are all created when the users are imported. To solve this on my test or reset scripts I need to turn off the creating a profile signal:

from django.db.models.signals import post_save
from django.contrib.auth.models import User
from users.models import create_profile # this is where my signal is registered
post_save.disconnect(create_profile, sender=User)

I will chalk that up as definite disadvantage for signals, perhaps there's a more cunning way around that.

Comments

There are no comments.

Login to add comments