Django REST Framework: difference between views and viewsets?

爷,独闯天下 提交于 2019-12-02 14:14:49

ViewSets and Routers are simple tools to speed-up implementing of your API, if you're aiming to standard behaviour and standard URLs.

Using ViewSet you don't have to create separate views for getting list of objects and detail of one object. ViewSet will handle for you in consistent way both list and detail.

Using Router will connect your ViewSet into "standarized" (it's not standard in any global way, just some structure that was implemented by creators of Django REST framework) structure of URLs. That way you don't have to create your urlpatterns by hand and you're guaranteed that all of your urls are consistent (at least on layer that Router is responsible for).

It looks like not much, but when implementing some huge api where you will have many and many urlpatterns and views, using ViewSets and Routers will make big difference.

For better explanation: this is code using ViewSets and Routers:

views.py:

from snippets.models import Article
from rest_framework import viewsets
from yourapp.serializers import ArticleSerializer

class ArticleViewSet(viewsets.ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

urls.py:

from django.conf.urls import url, include
from yourapp import views
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'articles', views.ArticleViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),
]

And equivalent result using normal Views and no routers:

views.py

from snippets.models import Article
from snippets.serializers import ArticleSerializer
from rest_framework import generics


class ArticleList(generics.ListCreateAPIView):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer


class ArticleDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

urls.py

from django.conf.urls import url, include
from yourapp import views

urlpatterns = [
    url(r'articles/^', views.ArticleList.as_view(), name="article-list"),
    url(r'articles/(?P<pk>[0-9]+)/^', views.ArticleDetail.as_view(), name="article-detail"),
]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!