parent
5aed96cb02
commit
a1e220a67f
8 changed files with 130 additions and 4 deletions
@ -0,0 +1,22 @@ |
||||
from rest_framework.views import exception_handler |
||||
|
||||
|
||||
def my_exception_handler(exc, context): |
||||
response = exception_handler(exc, context) |
||||
print('exception_handler') |
||||
print(exception_handler) |
||||
if response is not None: |
||||
response.data['code'] = response.status_code |
||||
if response.status_code == 405: |
||||
response.data['msg'] = '请求不予许' |
||||
elif response.status_code == 401: |
||||
response.data['msg'] = '认证未通过' |
||||
elif response.status_code == 403: |
||||
response.data['msg'] = '禁止访问' |
||||
elif response.status_code == 404: |
||||
response.data['msg'] = '未找到文件' |
||||
elif response.status_code >= 500: |
||||
response.data['msg'] = '服务器异常' |
||||
else: |
||||
response.data['msg'] = '其他未知错误' |
||||
return response |
@ -0,0 +1,72 @@ |
||||
from rest_framework.viewsets import ModelViewSet |
||||
from rest_framework import status |
||||
from common.myresponse import MyResponse |
||||
|
||||
|
||||
class MyModelViewSet(ModelViewSet): |
||||
|
||||
# CreateModelMixin -> create |
||||
def create(self, request, *args, **kwargs): |
||||
print(request.data) |
||||
serializer = self.get_serializer(data=request.data) |
||||
print(serializer) |
||||
serializer.is_valid(raise_exception=True) |
||||
print(serializer.data) |
||||
self.perform_create(serializer) |
||||
headers = self.get_success_headers(serializer.data) |
||||
return MyResponse(data=serializer.data, code=201, msg='OK', status=status.HTTP_201_CREATED, headers=headers) |
||||
|
||||
# ListModelMixin -> list |
||||
def list(self, request, *args, **kwargs): |
||||
print('self.get_queryset()') |
||||
print(self.get_queryset()) |
||||
queryset = self.filter_queryset(self.get_queryset()) |
||||
print('queryset') |
||||
print(queryset) |
||||
page = self.paginate_queryset(queryset) |
||||
print('page') |
||||
print(page) |
||||
if page is not None: |
||||
serializer = self.get_serializer(page, many=True) |
||||
print('serializer') |
||||
print(serializer) |
||||
print('serializer.data') |
||||
print(serializer.data) |
||||
return self.get_paginated_response(serializer.data) |
||||
serializer = self.get_serializer(queryset, many=True) |
||||
print('serializer') |
||||
print(serializer) |
||||
print('serializer.data') |
||||
print(serializer.data) |
||||
return MyResponse(data=serializer.data, code=200, msg='OK', status=status.HTTP_200_OK) |
||||
|
||||
# RetrieveModelMixin -> retrieve |
||||
def retrieve(self, request, *args, **kwargs): |
||||
instance = self.get_object() |
||||
print(instance) |
||||
serializer = self.get_serializer(instance) |
||||
print(serializer) |
||||
return MyResponse(data=serializer.data, code=200, msg='OK', status=status.HTTP_200_OK) |
||||
|
||||
# UpdateModelMixin -> update |
||||
def update(self, request, *args, **kwargs): |
||||
print(kwargs) |
||||
partial = kwargs.pop('partial', False) |
||||
print(partial) |
||||
instance = self.get_object() |
||||
print(instance) |
||||
print(request.data) |
||||
serializer = self.get_serializer(instance, data=request.data, partial=partial) |
||||
print(serializer) |
||||
serializer.is_valid(raise_exception=True) |
||||
self.perform_update(serializer) |
||||
|
||||
if getattr(instance, '_prefetched_objects_cache', None): |
||||
instance._prefetched_objects_cache = {} |
||||
return MyResponse(data=serializer.data, code=200, msg='OK', status=status.HTTP_200_OK) |
||||
|
||||
# DestroyModelMixin -> destroy |
||||
def destroy(self, request, *args, **kwargs): |
||||
instance = self.get_object() |
||||
self.perform_destroy(instance) |
||||
return MyResponse(data=[], code=200, msg='OK', status=status.HTTP_204_NO_CONTENT) |
Loading…
Reference in new issue