Django REST框架:视图和视图之间的区别? [英] Django REST Framework: difference between views and viewsets?

查看:518
本文介绍了Django REST框架:视图和视图之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是相关的< a>。



视图视图集有什么区别? 路由器 urlpatterns

解决方案

ViewSets 路由器是加快实施API的简单工具,如果您的目标是标准行为和标准URL。 / p>

使用 ViewSet ,您不必创建单独的视图来获取对象的列表和一个对象的详细信息。 ViewSet将以一致的方式处理您的列表和详细信息。



使用路由器将连接您的转换成标准化(它不是任何全局方式的标准,只是由Django REST框架的创建者实现的一些结构)URL的结构。这样你就不用手动创建你的urlpattern,你可以保证所有url都是一致的(至少在 Router 负责)上。



看起来不是很多,但是当实现一些巨大的api时,您将拥有许多和许多urlpatterns和视图,使用 ViewSets 路由器将有很大的区别。



更好的解释:这是使用ViewSets和路由器的代码:



views.py:

  from snippets.models import 
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 DefaultRo uter

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

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

等效结果使用正常视图和路由器:



views.py

 来自片段。导入文章
从snippets.serializers导入ArticleSerializer
从rest_framework import泛型


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_vi ew(),name =article-list),
url(r'articles /(?P< pk> [0-9] +)/ ^',views.ArticleDetail.as_view() article-detail),
]


May be relevant.

What is the difference between views and viewsets? And what about router and urlpatterns?

解决方案

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 
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"),
]

这篇关于Django REST框架:视图和视图之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆