Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between APIView class and generics.GenericAPIView

What is the difference between APIView class and generics.GenericAPIView

like image 616
Jyotiranajn Avatar asked Jun 14 '18 09:06

Jyotiranajn


People also ask

What is the difference between APIView and 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 .

Should I use APIView or ViewSet?

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 .

What is the difference between ViewSet and ModelViewSet?

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() .

What are generics in Rest_framework?

The generic views provided by REST framework allow you to quickly build API views that map closely to your database models.


2 Answers

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.

like image 51
Linovia Avatar answered Oct 02 '22 09:10

Linovia


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

like image 24
Ankush Sahu Avatar answered Oct 02 '22 07:10

Ankush Sahu