Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between APIView class and viewsets class?

People also ask

What is the difference between View and ViewSet in Django?

While regular views act as handlers for HTTP methods, viewsets give you actions, like create or list . The great thing about viewsets is how they make your code consistent and save you from repetition. Every time you write views that should do more than one thing, a viewset is the thing that you want to go for.

What is APIView in Django?

APIView class is a subclass of a Django View class. APIView classes are different from regular View classes in the following ways: Requests passed will be REST framework's Request instances, not Django's HttpRequest instances. Handler methods may return REST framework, instead of Django's HttpResponse.

What is Mixins in Django REST framework?

Mixins are a design pattern found in many languages. In Python, though the language does not support mixins natively,they are implemented using Python's multiple inheritence model. The mixins are different from inheritence. Inheritence is useful when you want to narrow a scope in the inherited class.


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

# 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

Because certain things like getting the /post/4, deleting /post/4, getting all posts, updating and creating new post was so common DRF provides ViewSets.

But first before you know ViewSets, let me tell you there are also Generic Classes that they do that things very good, but you need to provide full API end point like I did with my MyAPIView view (again for more info check http://www.cdrf.co/ or http://www.django-rest-framework.org/). So you would have to define your own urls path.

But with ViewSets you create ViewSet that actually merges all the above described operations and also you don't need to define url path you usually use a router that makes paths for you like:

 # views.py
 class PostViewSet(ViewSet):  # here you subclass Viewset check methods you can override, you have also ModelViewSet,...


 # urls.py 
 router = routers.DefaultRouter()
 router.register(r'post', PostViewSet, base_name='Post')

APIView allow us to define functions that match standard HTTP methods like GET, POST, PUT, PATCH, etc.

Viewsets allow us to define functions that match to common API object actions like : LIST, CREATE, RETRIEVE, UPDATE, etc.

Viewsets are also used to write logic to perform standard database operations and to interface with a database back-end. And are usually used for existing database model to manage predefined objects.


The functions you add to the APIView are different than the functions you add to the ViewSet class.

  • APIView: you add functions for the particular HTTP method you want to support on your endpoint. Ex: GET, POST, PUT, PATCH, DELETE

  • ViewSet: you would add functions that represent actions that you'd perform on a typical API. Ex: LIST, CREATE, RETRIEVE, UPDATE


Viewsets and APIView both allow us to write logic for end point but Viewsets dont define functions which map to HTTP methods instead map to common API object actions


APIView

  1. Present in rest_framework.views Module.

  2. Method Names reflect HTTP Methods like get(),post(),put(),patch(),delete()

  3. Map views to urls explicitly

  4. Length of the code is more

  5. API Development time is more

  6. Developer has complete control over the logic

  7. Best suitable for complex operations like using multiple data sources simultaneously, calling other APIs etc

ViewSet

  1. Present in rest_framework.viewsets Modules

  2. Method Names reflect Database Model class actions/operations like list(),retrieve(),create(),update(),partial_update() and destroy()

  3. not required to map views to URLs explicitly. DefaultRouter will takes care URL mapping automatically

  4. Length of the code is less

  5. API Development time is less

  6. Developer won't have complete control over the logic

  7. Best suitable for developing simple APIs like developing CRUD interface for database models