首页 > 其他 > 详细

[Django] ModelViewSet from rest_framework and Router

时间:2017-04-18 18:50:01      阅读:316      评论:0      收藏:0      [点我收藏+]

To build rest api easily, we can use ModelViewSet from rest_framework.

It provides GET, POST, DELETE, PUT methods.

from rest_framework.viewsets import ModelViewSet
from .serializers import ListSerializer, CardSerializer
from .models import List, Card

class ListViewSet(ModelViewSet):
    queryset = List.objects.all()
    serializer_class = ListSerializer

class CardViewSet(ModelViewSet):
    queryset = Card.objects.all()
    serializer_class = CardSerializer

 

To render those model view set, you can use router provided by rest_framework.

By default, it end with slash!

from .api import ListViewSet, CardViewSet
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(rlists, ListViewSet)
router.register(rcards, CardViewSet)

urlpatterns = router.urls

 

[Django] ModelViewSet from rest_framework and Router

原文:http://www.cnblogs.com/Answer1215/p/6728854.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!