Proposal: Signal connection via "my_app.MyModel"

92 views
Skip to first unread message

Yo-Yo Ma

unread,
May 9, 2012, 5:14:39 PM5/9/12
to Django developers
I'd like to suggest adding the ability to connect signals with models
by app label and model name.

Example API:

post_save.connect(
my_signal_handler,
sender='my_app.MyModel'
)

This would allow for better decoupling of code, as well as a far
cleaner dependency graph.

I'm not certain how this would affect custom signals. I'm primarily
speaking of the provided signals in ``db.models.signals``, but this
might be something that applies to custom signals as well.

Yo-Yo Ma

unread,
May 11, 2012, 2:08:24 PM5/11/12
to Django developers
I'd be willing to implement this, with some guidance.

Dougal Matthews

unread,
May 12, 2012, 4:24:53 AM5/12/12
to django-d...@googlegroups.com
On 9 May 2012 22:14, Yo-Yo Ma <baxters...@gmail.com> wrote:
> This would allow for better decoupling of code, as well as a far
> cleaner dependency graph.

The dependancy will still be there, the string containing the dotted path
would be a hard link. However, this will be hidden and a less obvious
dependancy than a normal Python import.

So, I'd be -1 as I don't see the advantage of this and rather it just adds
further complexity.

Yo-Yo Ma

unread,
May 13, 2012, 9:43:35 AM5/13/12
to Django developers
It would not be a "hard link". Do you know the definition of
"dependency graph", and do you know anything about Django's internals
or about how Django already allows you to connect models' foreign keys
this way? It would be simply "get me the model 'MyModel' from the app
'my_app'". The app could easily be moved around, and no extra imports
would be used in the process (see models.get_models()). This gives you
the ability to move the app around easily. The real benefit in my
oppinion would be the ability to connect signals for models from app A
inside of app B without having to worry about import order or errors,
due to some crisscross in app A's dependency graph.

On May 12, 4:24 am, Dougal Matthews <douga...@gmail.com> wrote:

Yo-Yo Ma

unread,
Aug 22, 2012, 1:17:14 AM8/22/12
to django-d...@googlegroups.com
Is there anyone else out there who doesn't like having to import models from app X into app Y just so that app Y can connect post save signals to them?

Russell Keith-Magee

unread,
Aug 22, 2012, 6:15:22 AM8/22/12
to django-d...@googlegroups.com
On Wed, Aug 22, 2012 at 1:17 PM, Yo-Yo Ma <baxters...@gmail.com> wrote:
> Is there anyone else out there who doesn't like having to import models from app X into app Y just so that app Y can connect post save signals to them?

I can't say the need to import models to set up a signal has ever
caused me any difficulty.

Yours,
Russ Magee %-)

Andrew Ingram

unread,
Aug 22, 2012, 6:47:49 AM8/22/12
to django-d...@googlegroups.com
I can see a use case for this, but would this not work?

from django.db.models import get_model
post_save.connect(
    my_signal_handler,
    sender=get_model('my_app', 'MyModel'),
)

As long as the app cache has been initialised before you call get_model, this should work fine.

- Andy


On 22 August 2012 06:17, Yo-Yo Ma <baxters...@gmail.com> wrote:
> Is there anyone else out there who doesn't like having to import models from app X into app Y just so that app Y can connect post save signals to them?
>
> --
> You received this message because you are subscribed to the Google Groups "Django developers" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/django-developers/-/_vcka4fdtPUJ.
> 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.
>

Reply all
Reply to author
Forward
0 new messages