简体   繁体   中英

'dict' object has no attribute '_committed' can anyone explain why this error occurs

I am sending files from the frontend and I want to save file location in database I am using DRF. Currently, I am trying to save only one file but i am getting error

"'dict' object has no attribute '_committed'" Can Anyone please give me some suggestions on what should I do? Request Body I am getting for on file:-

{'id': "Please provide Site Contact person's Name", 'service': {'id': 3, 'service_name': 'Site 
Survey', 'cost_per_kw': 1, 'min_size': 101, 'upto_size': 10000, 'tat': 5, 'creation_date': 
None}, 'question': 3, 'creation_date': None, 'real_estate': 0, 'answer': 'VIkrant Patil', 
'attachement': [{'name': 'Vikrant Patil - Offer Letter (1).pdf', 'size': 172518, 'url': 
'blob:http://localhost:8000/cad9de5d-a12b-41a2-90f7-38deff67fd0f', '_file': {}}], 'project': 
189}

My view.py logic for saving only one file:-

class ServiceProjectAnswerViewSet(viewsets.ModelViewSet):
model = ServiceProjectAnswer
serializer_class = ServiceProjectAnswerSerializer
# parser_classes = (MultiPartParser, FormParser)

def create(self, request, *args, **kwargs):
    print(request.data)
    instance = None
    answers = request.data.copy()
    data = {}
    attachements = answers['attachement']
    print(attachements[0])
    data['answer'] = answers['answer']
    data['project'] = answers['project']
    data['question'] = answers['question']
    serializer = self.get_serializer(data=data)
    if serializer.is_valid(raise_exception=True):
        try:
            instance = serializer.save()
            # for attachement in attachements:
            AnswerAttachments.objects.create(
                answer = instance,
                # attachement = attachement
                attachement = attachements[0]
            )
        except Exception as e:
            print(e)
            return Response(str(e), status=400)
    return Response(serializer.data, status=201)

My view.py logic for saving only multiple files:- In my responce when I a sending multiple files I am sending list of Objects every object has values which i want that is why I have use for loop to iterate.

def create(self, request, *args, **kwargs):
    instance = None
    print(request.data)
    answers = request.data.copy()
    for answer in answers:
        data = {}
        attachements = answer['attachement']
        print(attachements,"attachement")
        data['answer'] = answer['answer']
        data['project'] = answer['project']
        data['question'] = answer['question']
        print(data,"data")
        serializer = self.get_serializer(data=data)
        if serializer.is_valid(raise_exception=True):       
            try:
                instance=serializer.save()
                for attachement in attachements:
                    print(attachement)
                    # attach =  {}
                    # attach['name'] = attachement['name']  
                    # attach['size'] = attachement['size']
                    # attach['url'] = attachement['url']
                    print(type(attachement))
                    AnswerAttachments.objects.create(
                        answer=instance,
                        attachement=attachement
                    )   
            except Exception as e:
                print(e)
                return Response(str(e), status=400)
        else:
            return Response({'message': ('Failed to save answers')}, status=400)
    return Response('Answers are saved successfully!!', status=200)

Try something like this,

    const formData = new FormData();
    formData.append('file', file);

    const requestOptions = {
        method: "POST",
        body:formData
     };

Means,try to send body of request in multiform instead of application/json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM