What is the difference between APIView class and generics.GenericAPIView
GenericAPIView is a more loaded version of APIView . It isn't really useful on its own but can be used to create reusable actions. Mixins are bits of common behavior. They're useless without GenericAPIView .
APIView and ViewSet all have their right use cases. But most of the time, if you are only doing CRUD on resources, you can directly use ViewSets to respect the DRY principle. But if you are looking for more complex features, you can go low-level because after all, viewsets are also a subclass of APIView .
The ReadOnlyModelViewSet class also inherits from GenericAPIView . As with ModelViewSet it also includes implementations for various actions, but unlike ModelViewSet only provides the 'read-only' actions, .list() and .retrieve() .
The generic views provided by REST framework allow you to quickly build API views that map closely to your database models.
APIView
is a base class. It doesn't assume much and will allow you to plug pretty much anything to it.
GenericAPIView
is meant to work with Django's Models. It doesn't assume much beyond all the bells and whistles the Model introspection can provide.
APIView is the base class based view. Viewsets have APIView as a parent class.
With APIview you code methods etc for the different HTTP call methods such as post, get, put etc. These come with no standard configuration, so you can customise to your needs
With Viewsets, you code more specific methods . For example the ‘retrieve’ method, will expect arguments of the request and the pk of the object to be retrieved. Thus you write methods for create, retrieve, list, etc instead of post,get…
Moving on from that, you also have the routers. These work with viewsets and will create the urls for you based on how you coded the Viewset
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With