Difference between APIView class and view class?

What is the difference between the APIView class and the viewsets class? I follow the official Django REST-framework documentation. I think he lacks examples.

Can you explain the above difference with a suitable example.

+6
source share
1 answer

APIView is the most basic class that you usually override when defining a REST view. You usually define your methods such as get, put, delete, and others check ( http://www.cdrf.co/3.5/rest_framework.views/APIView.html ). With APIView, you define your view and add it to your URLs as follows:

#in views.py class MyAPIView(APIView): ... #here you put your logic check methods you can use #in urls.py url(r'^posts$', MyAPIView.as_view()), #List of all the posts 

Since some things, such as receiving / post / 4, deleting / post / 4, receiving all messages, updating and creating a new publication, were so common that DRF provides Viewsets.

But before you learn โ€œViewsetsโ€, let me tell you that there are also general classes, that they do it very well, but you need to provide a complete API endpoint, as in my MyAPIView view (again for additional verification of http information : //www.cdrf.co/ or http://www.django-rest-framework.org/ ). Therefore, you will need to define your own URL path.

But with ViewSets, you create a viewset that actually combines all the operations described above, and you donโ€™t need to specify the URL that you usually use for the router that creates the paths for you:

 #views.py class PostViewSet(ViewSet): #here you subclass Viwset check methods you can override, you have also ModelViewSet,... # urls.py router = routers.DefaultRouter() router.register(r'post', PostViewSet, base_name='Post') 
+7
source

Source: https://habr.com/ru/post/1013599/


All Articles