Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to change order of Django signals?

I've got 2 signals being sent at user registration, socialauth_registered and post_save. I'd like for socialauth_registered to precede post_save, as it affects the function that post_save triggers.

Is this possible? (and if it is, how?!)

I'm not sure what exactly is relevant, but I've got:

from django.contrib.auth.models import User
from social_auth.signals import socialauth_registered, pre_update
from django.db.models.signals import post_save

<ALL OF MY MODELS>

def create_user_profile(sender, instance, created, **kwargs):
    do some stuff

def create_social_profile(sender, user, response, details, **kwargs):
    do other stuff

socialauth_registered.connect(create_social_profile, sender=None)
post_save.connect(create_user_profile, sender=User)
like image 945
Matt Parrilla Avatar asked Apr 04 '12 19:04

Matt Parrilla


People also ask

Are Django signals synchronous?

First, to dispel a misconception about signals, they are not executed asynchronously. There is no background thread or worker to execute them. Like most of Django, they are fully "synchronous".

How do signals work in Django?

Django includes a “signal dispatcher” which helps decoupled applications get notified when actions occur elsewhere in the framework. In a nutshell, signals allow certain senders to notify a set of receivers that some action has taken place.

How many types of signals are there in Django?

There are 3 types of signal. pre_save/post_save: This signal works before/after the method save(). pre_delete/post_delete: This signal works before after delete a model's instance (method delete()) this signal is thrown. pre_init/post_init: This signal is thrown before/after instantiating a model (__init__() method).

Should I use Django signals?

The only reason to use signalsOnly use signals to avoid introducing circular dependencies. If you have two apps, and one app wants to trigger behaviour in an app it already knows about, don't use signals. The app should just import the function it needs and call it directly.


1 Answers

No, you cannot change the order that signals are executed.

Signal priority has been proposed, but the core developers have said they will not implement this feature:

https://code.djangoproject.com/ticket/16547

like image 64
Derek Curtis Avatar answered Oct 20 '22 04:10

Derek Curtis