diff --git a/django_celery_beat/apps.py b/django_celery_beat/apps.py index 0a783d21..65084ef2 100644 --- a/django_celery_beat/apps.py +++ b/django_celery_beat/apps.py @@ -12,3 +12,7 @@ class BeatConfig(AppConfig): label = 'django_celery_beat' verbose_name = _('Periodic Tasks') default_auto_field = 'django.db.models.AutoField' + + def ready(self): + from .signals import signals_connect + signals_connect() diff --git a/django_celery_beat/models.py b/django_celery_beat/models.py index 7cb8d0b4..93b80d8d 100644 --- a/django_celery_beat/models.py +++ b/django_celery_beat/models.py @@ -619,23 +619,3 @@ def schedule(self): return self.solar.schedule if self.clocked: return self.clocked.schedule - - -signals.pre_delete.connect(PeriodicTasks.changed, sender=PeriodicTask) -signals.pre_save.connect(PeriodicTasks.changed, sender=PeriodicTask) -signals.pre_delete.connect( - PeriodicTasks.update_changed, sender=IntervalSchedule) -signals.post_save.connect( - PeriodicTasks.update_changed, sender=IntervalSchedule) -signals.post_delete.connect( - PeriodicTasks.update_changed, sender=CrontabSchedule) -signals.post_save.connect( - PeriodicTasks.update_changed, sender=CrontabSchedule) -signals.post_delete.connect( - PeriodicTasks.update_changed, sender=SolarSchedule) -signals.post_save.connect( - PeriodicTasks.update_changed, sender=SolarSchedule) -signals.post_delete.connect( - PeriodicTasks.update_changed, sender=ClockedSchedule) -signals.post_save.connect( - PeriodicTasks.update_changed, sender=ClockedSchedule) diff --git a/django_celery_beat/signals.py b/django_celery_beat/signals.py new file mode 100644 index 00000000..e5bedfc6 --- /dev/null +++ b/django_celery_beat/signals.py @@ -0,0 +1,41 @@ +def signals_connect(): + from django.db.models import signals, OneToOneRel + from .models import ClockedSchedule, PeriodicTask, PeriodicTasks, IntervalSchedule, CrontabSchedule, SolarSchedule + + def o2o_discover(): + """ + Description: Discover the `OneToOneField`, and connect their signals to `PeriodicTasks.changed`. + + Issues: Signals can not connect to OneToOneField. + https://github.com/celery/django-celery-beat/issues/572 + + Note: The inheritance relationship introduces links between the child model and each of its + parents (via an automatically-created OneToOneField). + https://docs.djangoproject.com/en/stable/topics/db/models/#multi-table-inheritance + """ + related_objects = PeriodicTask._meta.related_objects + for obj in related_objects: + if isinstance(obj, OneToOneRel): + sender_class = obj.related_model + signals.pre_delete.connect(PeriodicTasks.changed, sender=sender_class) + signals.pre_save.connect(PeriodicTasks.changed, sender=sender_class) + + o2o_discover() + signals.pre_delete.connect(PeriodicTasks.changed, sender=PeriodicTask) + signals.pre_save.connect(PeriodicTasks.changed, sender=PeriodicTask) + signals.pre_delete.connect( + PeriodicTasks.update_changed, sender=IntervalSchedule) + signals.post_save.connect( + PeriodicTasks.update_changed, sender=IntervalSchedule) + signals.post_delete.connect( + PeriodicTasks.update_changed, sender=CrontabSchedule) + signals.post_save.connect( + PeriodicTasks.update_changed, sender=CrontabSchedule) + signals.post_delete.connect( + PeriodicTasks.update_changed, sender=SolarSchedule) + signals.post_save.connect( + PeriodicTasks.update_changed, sender=SolarSchedule) + signals.post_delete.connect( + PeriodicTasks.update_changed, sender=ClockedSchedule) + signals.post_save.connect( + PeriodicTasks.update_changed, sender=ClockedSchedule)