Home > other >  Validation error ['clocked must be one off, one_off must set True']
Validation error ['clocked must be one off, one_off must set True']

Time:11-25

I am trying to save periodic tasks using django_celery_beat package getting the above error, the code is as follows. Save uses post save signals

class MailTask(models.Model):
    name = models.CharField(max_length=100, blank=True)
    subject = models.CharField(max_length=100)
    message = models.TextField()
    from_email = models.EmailField()
    recipient = models.EmailField()
    sent = models.BooleanField(default=False)
    time_to_send = models.DateTimeField()

def schedule_mail_task(sender, instance, created, **kwargs):
    if created:
        clocked_schedule, created = ClockedSchedule.objects.get_or_create(
            clocked_time=instance.time_to_send,
        )
        periodic_task = PeriodicTask.objects.create(
            clocked=clocked_schedule,
            name=f'instance.name {instance.timestamp}',
            task='core.tasks.send_mail_func',
            enabled=True,
        )


post_save.connect(schedule_mail_task, sender=MailTask)

CodePudding user response:

As the error says, you need to set your task to one-off:

periodic_task = PeriodicTask.objects.create(
        clocked=clocked_schedule,
        name=f'instance.name {instance.timestamp}',
        task='core.tasks.send_mail_func',
        enabled=True,
        one_off=True,
    )
  • Related