Thoughts on defining and autoimporting signals.py

1,463 views
Skip to first unread message

Daniel Sokolowski

unread,
Dec 21, 2011, 2:58:20 PM12/21/11
to Django developers
Dear Django Developers,

Is there any consensus on where to define your signals for models and
how to import them? I ask because to me it seems very odd that there
doesn't appear to be a standard way to add your own signals and have
them auto imported by the framework. I for example create a /
project_root/project_app/signals.py file and auto import it in the
__init__ of my project app.

I have read and searched the https://docs.djangoproject.com/en/dev/topics/signals/
however it fails short to provide any gudiance on this.

How do you do it, what are your thoughts on the making the auto import
and signals.py file standard? Has this been already discussed?

Brendan Smith

unread,
Dec 21, 2011, 3:20:13 PM12/21/11
to django-d...@googlegroups.com
wrong list, you want django-users

but yes, you are on the right track with importing from __init__, at least that's how i've always done it

-- 
Brendan Smith
IT Specialist
National Priorities Project
243 King Street Suite 109
Northampton, MA 01060
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.

Russell Keith-Magee

unread,
Dec 21, 2011, 9:49:44 PM12/21/11
to django-d...@googlegroups.com
On Thu, Dec 22, 2011 at 3:58 AM, Daniel Sokolowski
<daniel.s...@klinsight.com> wrote:
> Dear Django Developers,
>
> Is there any consensus on where to define your signals for models and
> how to import them? I ask because to me it seems very odd that there
> doesn't appear to be a standard way to add your own signals and have
> them auto imported by the framework. I for example create a /
> project_root/project_app/signals.py file and auto import it in the
> __init__ of my project app.
>
> I have read and searched the https://docs.djangoproject.com/en/dev/topics/signals/
> however it fails short to provide any gudiance on this.

Yes - this is a known problem. The root problem is that there isn't a
single reliable place to put "app startup" logic. With the current way
Django handles apps, there isn't a single solution that will work
everywhere, which one of the reasons that the docs are silent on the
issue.

The good news is that there is a plan to rectify this -- Arthur
Koziel's App Refactor patch, largely created during the 2010 Google
Summer of Code, and then polished by Jannis Leidel, provides a generic
"entry point" for an app, which would be a suitable place to put logic
such as signal registration.

The bad news is that the patch has been languishing in need of review
for a while. At this point, it almost certainly won't make 1.4; if it
can get some attention, it would be a welcome addition to 1.5.

Yours,
Russ Magee %-)

Emil Stenström

unread,
Jan 19, 2012, 8:44:00 AM1/19/12
to django-d...@googlegroups.com

On Thursday, 22 December 2011 03:49:44 UTC+1, Russell Keith-Magee wrote:

... there isn't a single solution that will work


everywhere, which one of the reasons that the docs are silent on the
issue.

Just for the record: The docs are actually telling you to put your signals in models.py, at the end of: https://docs.djangoproject.com/en/dev/topics/signals/#s-receiver-functions

I would much rather have them point people to signals.py, and an import signals.py in __init__.py. I've seen beginners be trapped in the circular import loop when trying to move their signals to a separate file, not realizing that they also need to move the import of signals.py away from models to be able to use Model classes inside signals.

Daniel Sokolowski

unread,
Jan 19, 2012, 10:14:20 AM1/19/12
to django-d...@googlegroups.com
+1,
 
I think they ought to be auto imported just like models.py, views.py, etc. are.
--
You received this message because you are subscribed to the Google Groups "Django developers" group.

Kirit Sælensminde (kayess)

unread,
Jan 20, 2012, 9:13:46 PM1/20/12
to Django developers


On Jan 19, 8:44 pm, Emil Stenström <e...@kth.se> wrote:
> On Thursday, 22 December 2011 03:49:44 UTC+1, Russell Keith-Magee wrote:
> > ... there isn't a single solution that will work
> > everywhere, which one of the reasons that the docs are silent on the
> > issue.
>
> Just for the record: The docs are actually telling you to put your signals
> in models.py, at the end of:https://docs.djangoproject.com/en/dev/topics/signals/#s-receiver-func...
>
> I would much rather have them point people to signals.py, and an import
> signals.py in __init__.py. I've seen beginners be trapped in the circular
> import loop when trying to move their signals to a separate file, not
> realizing that they also need to move the import of signals.py away from
> models to be able to use Model classes inside signals.

The problem with importing them from __init__.py is that it breaks
coverage as the signals and the models get imported before the
coverage tools are loaded when using things nose, django nose and nose
coverage.


Kirit

Ihor Kaharlichenko

unread,
Jan 15, 2014, 9:01:11 AM1/15/14
to django-d...@googlegroups.com
Hi.

The bad news is that the patch has been languishing in need of review
for a while. At this point, it almost certainly won't make 1.4; if it
can get some attention, it would be a welcome addition to 1.5.

It's already 1.6 now, are there any news regarding this issue? From what I see the aren't any updates in signals docs. Maybe there is a ticket one could track?

It would be really nice if there was an official, "the Django way" for where to put signals, where to put handlers, where to wire them up, and how to test all that stuff.

Regards, Ihor.

Aymeric Augustin

unread,
Jan 15, 2014, 3:46:03 PM1/15/14
to django-d...@googlegroups.com
On 15 janv. 2014, at 15:01, Ihor Kaharlichenko <madk...@gmail.com> wrote:

> It's already 1.6 now, are there any news regarding this issue?

It’s fixed in 1.7. Search for “app-loading” in the archives of this list.

> From what I see the aren't any updates in signals docs.

Look again: https://github.com/django/django/commits/master/docs/ref/signals.txt

> It would be really nice if there was an official, "the Django way" for where to put signals, where to put handlers, where to wire them up, and how to test all that stuff.

There will be in 1.7: https://github.com/django/django/commit/c31d7c48139260ccb72deda9b0033db0db86e84a

--
Aymeric.




Reply all
Reply to author
Forward
0 new messages