[Fixed]-What is the difference in use or inherit from APIView and Model ViewSet

29👍

Question is too open though I’ll try to answer it.

First thing first, APIView or ViewSet aren’t tied to models while ModelViewSet, GenericAPIView, ListAPIView (and co) are.

The major difference between *View and *ViewSet is that *ViewSet are meant to work with routers and provide a single class to expose a Resource while *View will require two (one for list/create, another for detail/update/delete).

Note that APIView is the most lower level and will only tie to HTTP verbs (get/post/put…) while ViewSet or GenericAPIView will have CRUD such as list / update…

In order to expose a Django’s Model, you’ll need either

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

or

class UserListCreateView(ListCreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

class UserRetrieveUpdateDestroyView(RetrieveUpdateDestroyAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

Leave a comment