'filefield serializer is not parsing the file object

I have a model in my application:

class GRNRFIDSerials(models.Model):

    grn = models.ForeignKey(Grn, on_delete=models.CASCADE)
    file = models.FileField(upload_to='grnrfid/', null=True, blank=True)
    owner = models.ForeignKey(User, on_delete=models.CASCADE)

I am trying to upload the file in the ORM like the following:

class GRNRFIDSerialsUploadAPIView(CreateAPIView):

    permission_classes = (permissions.IsAuthenticated, )
    serializer_class = GrnRFIDSerialsSerializer
    parser_classes = (FormParser, MultiPartParser)

    def post(self, request, *args, **kwargs):
        owner = request.user.pk
        print("reached")
        d = request.data.copy()
        d['owner'] = owner
        print("d", d)
        serializer = GrnRFIDSerialsSerializer(data=d)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

where d is :

d <QueryDict: {'no_of_fileA_files': ['1'], 'grn': ['479'], 'viewType': ['Pool Operator'], 'companyId': ['52'], 
'document': [<InMemoryUploadedFile: EPC (4).xlsx (application/vnd.openxmlformats-officedocument.spr
    eadsheetml.sheet)>], 'owner': [27]}>

This runs without an error but n the database entry filefield is shown as blank. How do I upload the file object?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source