Skip to content

Commit

Permalink
Fixed signals can not connect to OneToOneField (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
954-Ivory committed Aug 21, 2022
1 parent d4b97af commit b99abe3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
4 changes: 4 additions & 0 deletions django_celery_beat/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
20 changes: 0 additions & 20 deletions django_celery_beat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
41 changes: 41 additions & 0 deletions django_celery_beat/signals.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit b99abe3

Please sign in to comment.