Django post_syncdb signal handler not getting called?
I have a myapp/management/__init__.py
that i开发者_JAVA百科s registering a post_syncdb
handler like so:
from django.db.models import signals
from features import models as features
def create_features(app, created_models, verbosity, **kwargs):
print "Creating features!"
# Do stuff...
signals.post_syncdb.connect(create_features, sender=features)
I've verified the following:
- Both
features
andmyapp
are insettings.INSTALLED_APPS
myapp.management
is getting loaded prior to the syncdb running (verified via a print statement at the module level)- The
features
app is getting processed bysyncdb
, and it is emitting apost_syncdb
signal (verified by examiningsyncdb
's output with--verbosity=2
. - I'm using the exact same idiom for another pair of apps, and that handler is called correctly. I've compared the two modules and found no relevant differences between the invocations.
However, myapp.management.create_features
is never called. What am I missing?
try putting it in your models.py
Just came across the same issue, and the way I solved it was to remove the sender
from function arguments and check for it inside the callback function.
from django.db.models import signals
from features import models as features
def create_features(app, created_models, verbosity, **kwargs):
print "Creating features!"
if app != features #this will work as it compares models module instances
return
# Do stuff...
signals.post_syncdb.connect(create_features)
That way you can keep them in your management module like Django docs suggest. I agree that it should work like you suggested. You could probably dig into the implementation of the Signal class in django.dispatch
.
The point is in the sender
. Your custom callback is called only if sender
worked out. In my case the sender
was db.models
and it is not worked out if syncdb called not the first time, i.o. the synced models are exist in database. In the documents it is written, but do not putted a proper emphasis.
sender
The models module that was just installed. That is, if syncdb just installed an app called "foo.bar.myapp", sender will be the foo.bar.myapp.models module.
So my solution was drop the database and install my app again.
精彩评论