Created
January 26, 2018 17:32
-
-
Save toransahu/dfc2666307c1628042b99edaba630c07 to your computer and use it in GitHub Desktop.
Django REST framework VIEW Implementation Approach: Class Based + Inherit generics.GenericAPIView & mixin classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from snippets.models import Snippet | |
from snippets.serializers import SnippetSerializer | |
from rest_framework import mixins [ from rest_framework import mixins] | |
from rest_framework import generics [ from rest_framework import generics] | |
class SnippetList(mixins.ListModelMixin, | |
mixins.CreateModelMixin, | |
generics.GenericAPIView): | |
queryset = Snippet.objects.all() | |
serializer_class = SnippetSerializer | |
def get(self, request, *args, **kwargs): | |
return self.list(request, *args, **kwargs) | |
def post(self, request, *args, **kwargs): | |
return self.create(request, *args, **kwargs) | |
class SnippetDetail(mixins.RetrieveModelMixin, | |
mixins.UpdateModelMixin, | |
mixins.DestroyModelMixin, | |
generics.GenericAPIView): | |
queryset = Snippet.objects.all() | |
serializer_class = SnippetSerializer | |
def get(self, request, *args, **kwargs): | |
return self.retrieve(request, *args, **kwargs) | |
def put(self, request, *args, **kwargs): | |
return self.update(request, *args, **kwargs) | |
def delete(self, request, *args, **kwargs): | |
return self.destroy(request, *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment