Django Rest Framework: serializer response error
我在DRF中的视图集有问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | def get_queryset(self): """ :return: filtered queryset based on request query_param """ # todo#amedeo: improve the readability code qs = super(ChecklistViewSet, self).get_queryset() _pk = self.kwargs['pk'] if self.request.method == 'PUT': instance = qs.filter(pk=int(_pk)).first() # pass in the instance we want to update serializer = ChecklistSerializer(instance, self.request.data) # validate and update if serializer.is_valid(): serializer.save() serializer_dict = serializer.data serializer_dict["message"] ="Checklist updated successfully." return response.Response(serializer_dict, status=status.HTTP_200_OK) else: return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
在我的代码中,objetc被保存,但是响应给出了一个错误,即:
AttributeError: 'Response' object has no attribute 'model'
我的序列化程序是:
1 2 3 4 5 6 7 | class ChecklistSerializer(serializers.ModelSerializer): class Meta: model = Checklist fields = ('id', 'approved_by', 'role', 'hardship_classification', 'total_capacity', 'capacity_for_residents', 'checklist_type', 'state', 'pdf', 'submitting_afp', 'disabled', 'location') |
我通过传递json的client-put方法调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 | { "approved_by":"Test", "role":"test_role_4", "hardship_classification":"test_6", "total_capacity":"50", "capacity_for_residents":"350", "checklist_type":"permanent", "state":"qcl_draft", "pdf": null, "submitting_afp": 3999, "disabled": false, "location": 97 } |
但我收到了那个错误。模型已保存,但响应给出了错误
Internal Server Error: /api/v1/checklists/3/
Traceback (most recent call last):
File"/home/adifilippo/.envs/accomodation/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File"/home/adifilippo/.envs/accomodation/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File"/home/adifilippo/.envs/accomodation/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File"/home/adifilippo/.envs/accomodation/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File"/home/adifilippo/.envs/accomodation/local/lib/python2.7/site-packages/rest_framework/viewsets.py", line 116, in view
return self.dispatch(request, *args, **kwargs)
File"/home/adifilippo/.envs/accomodation/local/lib/python2.7/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File"/home/adifilippo/.envs/accomodation/local/lib/python2.7/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File"/home/adifilippo/.envs/accomodation/local/lib/python2.7/site-packages/rest_framework/views.py", line 483, in dispatch
self.initial(request, *args, **kwargs)
File"/home/adifilippo/.envs/accomodation/local/lib/python2.7/site-packages/rest_framework/views.py", line 401, in initial
self.check_permissions(request)
File"/home/adifilippo/.envs/accomodation/local/lib/python2.7/site-packages/rest_framework/views.py", line 334, in check_permissions
if not permission.has_permission(request, self):
File"/home/adifilippo/.envs/accomodation/local/lib/python2.7/site-packages/rest_framework/permissions.py", line 206, in has_permission
perms = self.get_required_permissions(request.method, queryset.model)
AttributeError: 'Response' object has no attribute 'model'
"PUT /api/v1/checklists/3/ HTTP/1.1" 500 42934
在这里输入代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | def put(self, request, pk, format=None): instance = self.get_object(pk) # pass in the instance we want to update serializer = ChecklistSerializer(instance, self.request.data) # validate and update if serializer.is_valid(): serializer.save() serializer_dict = serializer.data serializer_dict["message"] ="Checklist updated successfully." return response.Response(serializer_dict, status=status.HTTP_200_OK) else: return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |