无法设置 PeriodicTask 计划

Cannot set PeriodicTask schedule

我使用 Django Celery Beat 来管理周期性任务。

在这里,我创建了一个周期性任务并为其设置了一些时间表:

        new_instance = PeriodicTask.objects.create(
            name=f'L1 Synchonisation created at {timezone.now()}',
            task='integrations.tasks.test_task',
        )
        schedule, _ = IntervalSchedule.objects.get_or_create(
            every=self.every,
            period=self.period
        )
        new_instance.interval = schedule
        print(new_instance.interval)  # new_instance.interval is not None
        new_instance.save()
        print(new_instance.interval)  # None
        new_instance.refresh_from_db()
        print(new_instance.interval)  # None        

我的问题是,为什么 new_instance.interval 会变成 None,我该如何防止它变成这样?

原来我无法更改PeriodicTask的日程安排。实例化时只能同时设置PeriodicTask

        schedule, _ = IntervalSchedule.objects.get_or_create(
            every=self.every,
            period=self.period
        )
        new_instance = PeriodicTask.objects.create(
            name=f'L1 Synchonisation created at {timezone.now()}',
            task='integrations.tasks.test_task',
            interval=schedule,
        )
        print(new_instance.interval)  # new_instance.interval is not None
        new_instance.refresh_from_db()
        print(new_instance.interval)  # new_instance.interval is not None