#3495 closed (worksforme)
created_models doesn't match post_syncdb's app
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Keywords: | ||
Cc: | not.com@… | Triage Stage: | Design decision needed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When post_syncdb triggers a signal for one app, the "created_models" includes models created in other apps.
from django.dispatch import dispatcher from django.db.models import signals def show_it(sender, app, created_models, verbosity, interactive): print app, created_models dispatcher.connect(show_it,signal=signals.post_syncdb)
Put that in the init.py in one app of a multi-app project and try running "manage.py test"- notice that for every app, show_it is called with "created_models" that don't necessarily match "app".
Change History (4)
comment:1 by , 18 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:2 by , 18 years ago
Thanks, I suspect it's not the dispatcher but the signal issuer that's responsible for created_models. And the real issue for me isn't that there are non-app models listed, but that some models get listed repeatedly, as if they're created more than once.
I already have work-arounds, perhaps documenting the behavior is enough, "just thought you'd like to know."
comment:3 by , 18 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
Actually, I think the issue here is that you're listening for every instance of the post_syncdb
signal, when what you want is only instances of post_syncdb
which apply to a single specific app. You can do that by adding the keyword argument sender
to dispatcher.connect
; for example, the auth app knows to fire off create_superuser
by doing:
from django.contrib.auth import models as auth_app # ...snip... dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb)
created_models
, IIRC, is a growing list of all models syncdb
has installed so far, so you can check for specific models in the list if you're interested in them (and the auth app does this as well, by checking whether the user model is in created_models
before trying to instantiate and save one).
comment:4 by , 18 years ago
No that's not the issue, I want to process all models, regardless of app at syncdb time, and I want to process each model only once. I've edited the wiki at http://code.djangoproject.com/wiki/Signals for clarity.
I think this is by design. The signal dispatcher does not care about applications. But this is necessary to implemenent certain types of cross-application behaviour (e.g., logging each object creation or modification).
But I pass it on for core to decide.