The create/retrieve/update/delete operations that we've been using so far are going to be pretty similar for any model-backed API views we create. Those bits of common behaviour are implemented in REST framework's mixin classes.
We'll take a moment to examine exactly what's happening here. We're building our view using GenericAPIView
, and adding in ListModelMixin
and CreateModelMixin
.
class SnippetDetail(mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView):
serializer_class = SnippetSerializer
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
return self.update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
Pretty similar. Again we're using the GenericAPIView
class to provide the core functionality, and adding in mixins to provide the .retrieve()
, .update()
and .destroy()
actions.