Home > OS >  IntegrityError at /api/mail-task/ NOT NULL constraint failed: django_celery_beat_periodictask.task
IntegrityError at /api/mail-task/ NOT NULL constraint failed: django_celery_beat_periodictask.task

Time:11-28

Getting the above error while connecting a celery task with view

@shared_task
def send_mail_func(mail_task_id):
    mail_task = MailTask.objects.get(id=mail_task_id)
    subject = mail_task.subject
    message = mail_task.message
    from_email = mail_task.from_email
    recipient = mail_task.recipient
    send_mail(subject, message, from_email, [recipient], fail_silently=False)

class MailTaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = MailTask
        fields = '__all__'

class MailTaskCreateAPIView(CreateAPIView):
    serializer_class = MailTaskSerializer

    def perform_create(self, serializer):
        serializer.save()
        time_to_send = serializer.data['time_to_send']
        clocked_schedule, created = ClockedSchedule.objects.get_or_create(
            clocked_time=time_to_send
        )
        periodic_task = PeriodicTask.objects.create(
            clocked=clocked_schedule,
            name=create_random_unique_number(),
            task=send_mail_func(serializer.data['id']),
            one_off=True,
        )

Id is not passing to the view this way, how I can pass the id to create view, any help is much appreciated

CodePudding user response:

id can be passed as kwargs, try this way

 periodic_task = PeriodicTask.objects.create(
            clocked=clocked_schedule,
            name=create_random_unique_number(),
            task='app_name.tasks.taskname',
            kwargs=json.dumps({'mail_task_id': serializer.data['id']}),
            one_off=True,
        )
  • Related