[Python] Query returns None in Celery tasks

Discussão em 'Python' iniciado por Stack, Março 4, 2025.

  1. Stack

    Stack Membro Participativo

    I have a celery tasks in my application but when I try to query the database in the functions, some of them returns Does not exist or None


    @shared_task
    def email_on_assign_reviewer(submission, reviewerlist):
    """
    Send an email to the Reviewer when they are assigned to review
    """
    print(submission) # prints normally
    print(reviewerlist) # this prints the list of id of reviewers perfectly
    submission = Submission.objects.get(id=submission)
    print(submission) # this prints the submission perfectly

    context = {"some": "context"}

    for id in reviewerlist:
    reviewer = Reviewer.objects.filter(id=id).first()
    print(reviewer) #this returns none
    if reviewer:
    context['reviewer_name'] = reviewer.user.first_name
    utils.sendEmail(
    context,
    template="review_assign.html",
    to=[reviewer.user.email]
    )


    MY VIEW

    def perform_create(self, serializer):
    submission = Submission.objects.filter(id=self.kwargs['submission_pk']).first()

    *SOME LOGIC*

    data = serializer.save()
    for i in reviewerlist:
    print(Reviewer.objects.filter(id=i).first()) # this prints the reviewers perfectly

    tasks.email_on_assign_reviewer.delay(submission.id, reviewerlist)


    MY SERIALIZER

    def create(self, validated_data):
    submission_id = self.context['submission_id']
    user_ids = validated_data.pop('user_ids', [])
    existing_profile_ids = Somequery.objects.all()
    for user_id in user_ids:
    if user_id not in existing_profile_ids:
    reviewer = Reviewer.objects.create(submission_id=submission_id, user_id=user_id, )
    reviewers_to_create.append(reviewer.pk)

    return reviewers_to_create


    In some tasks, the submission returns DoesnotExist

    Continue reading...

Compartilhe esta Página