首页 > 代码库 > [Django] ModelViewSet from rest_framework and Router
[Django] ModelViewSet from rest_framework and Router
To build rest api easily, we can use ModelViewSet from rest_framework.
It provides GET, POST, DELETE, PUT methods.
from rest_framework.viewsets import ModelViewSetfrom .serializers import ListSerializer, CardSerializerfrom .models import List, Cardclass ListViewSet(ModelViewSet): queryset = List.objects.all() serializer_class = ListSerializerclass 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, CardViewSetfrom rest_framework.routers import DefaultRouterrouter = DefaultRouter()router.register(r‘lists‘, ListViewSet)router.register(r‘cards‘, CardViewSet)urlpatterns = router.urls
[Django] ModelViewSet from rest_framework and Router
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。