-1

I'm trying to pass a few arguments form my view to my additional function, but I get error "Object of type 'Request' is not JSON serializable", I don't why is occured with me, cause I didn't pass request object to Response.

@api_view(['POST', ])
@permission_classes([IsAuthenticated])
def users_upload(request):
    if request.user.type != 't':
        logger.warning(f'Users upload ERROR: -{request.user.username}- not a teacher')
        return Response({'message': 'Not a teacher'}, status=403)

    try:
        user_file = request.FILES['data']
        file_format = user_file.name.split('.')[-1]

        if file_format not in ['csv', 'xls', 'xlsx']:
            logging.error('User upload ERROR: incorrect file format')
            return Response({'message': 'Incorrect file format'}, status=400)

        auto_file = AutoFile.objects.create(
            description='Temp file for user upload',
            file=user_file
        )

        upload_users_from_file.delay(request, auto_file.pk, file_format)

        return Response({'message': 'Task created'}, status=200)

    except Exception as e:
        logging.error(f'User upload ERROR: unable to evaluate file format: {e}')
        return Response({'message': 'Error during file check'}, status=400)

What happened?

enter image description here I can't show function "upload_users_from_file" because it distributes request object to large functions chain, anyway you can see by logger that ERROR occured actually in user_upload function.

shevdenvla
  • 55
  • 6
  • What is `upload_users_from_file`? You pass `request` to this function. Also show the _complete_ error traceback. – Abdul Aziz Barkat Apr 19 '21 at 08:14
  • @Abdul Aziz Barkat, I edited my question, also can you tell my where I can see complete traceback if I running my app in docker containers? – shevdenvla Apr 19 '21 at 08:25
  • "_anyway you can see by logger that ERROR occured actually in user_upload function._" that is not necessary you see the log there because you catch it in that function, as I say `upload_users_from_file.delay` is the most likely place where the error occurs. Also how would you see the error if you catch it? Also see [Python app does not print anything when running detached in docker](https://stackoverflow.com/questions/29663459/python-app-does-not-print-anything-when-running-detached-in-docker) – Abdul Aziz Barkat Apr 19 '21 at 08:38
  • @Abdul Aziz Barkat, yes, you're right, when I replace delay() method I get correct behavior programm, but I want use delay in this place. Do you know any suggestions? – shevdenvla Apr 19 '21 at 08:48

1 Answers1

2

When delay() Celery must to serialize all the delay arguments to store in the task database. When Celery-worker gets this task, it deserializes arguments.

You try to pass the Request object as an argument to delay. Celery doesn't known how to serialize this type of objects.

Some advises:

  1. Try to pass to tasks only objects that serializable by default - lists, dicts, strs, ints, etc. If you need to pass a lot of arguments, combine them into dicts or tuples.

  2. You may want to make your object serializable. Please pay attention to these topics: