Search code examples
djangodjango-ormdjango-signals

How to trigger Django Signals in sequence


I am firing a signal which has 3 different receivers. What I want to do is to update a table Student using the signal update_student and later on I want to update his enrollment in update_student_enrollment.

I want to update Enrollment after the Student has been updated. But my update enrollment receiver is triggering before the student is updated.

Sending the signal.

Signal.send("student_updated", student_id=1, active=active)

Receiver 1

@receiver(student_updated)
def update_student(sender, **kwargs):
    Student.objects.update(active=0) # I am setting the student activation to false. For simplicity I am not mentioning the logic which is setting the student to inactive.    
    print("Student Updated!")

Receiver 2

@receiver(student_updated)
def update_student_enrollment(sender, **kwargs):
    student=Student.objects.filter(student_id=1)
    if student.active=0:
        StudentEnrollment.objects.filter(student_id=1).update(active=0)

Somehow my Receiver 2 is firing before the Receiver 1.


Solution

  • It's not possible to explicitly specify the ordering of signals in Django.

    The best way you can handle this is by having the Receiver 1 signal send a signal itself on which you can let Receiver 2 listen.