You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.8 KiB
72 lines
2.8 KiB
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)
|
|
|