码迷,mamicode.com
首页 > Windows程序 > 详细

RESTful API终极版序列化封装

时间:2018-08-02 22:48:42      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:serial   view   delete   res   article   creat   get   rtc   import   

urls:

from django.conf.urls import url
from app01 import views

urlpatterns = [
    # url(r"comment/$",views.Comment.as_view()),
    url(r"comment/(?P<pk>\d+)",views.CommentDetailView.as_view()),
    url(r"article/$",views.Article.as_view()),
    url(r"school/(?P<id>\d+)",views.SchoolDetail.as_view(),name="school-detail"),
    url(r"school_view/$",views.SchoolView.as_view(),name="school_view-detail"),
    url(r"comment_view/$",views.CommentView.as_view(),name="comment_view-detail"),
    url(r"article/(?P<pk>\d+)",views.ArticleDetail.as_view(),name="article-detail"),


    # url(r‘comment/$‘,views.CommentViewSet.as_view({
    #     "get":"list",
    #     "put":"create",
    # })),
    #
    # url(r‘comment/(?P<pk>\d+)/$‘,views.CommentViewSet.as_view({
    #     "get":"retrieve",
    #     "put":"update",
    #     "delete":"destroy"
    # }))

]

from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(rcomment,views.CommentViewSet)
urlpatterns += router.urls


#   相当于 在urlpatterns里添加了:
    # url(r‘comment/$‘,views.CommentViewSet.as_view({
        #     "get":"list",
        #     "put":"create",
        # })),
        #
        # url(r‘comment/(?P<pk>\d+)/$‘,views.CommentViewSet.as_view({
        #     "get":"retrieve",
        #     "put":"update",
        #     "delete":"destroy"
        # }))

 

views:此处为RESTful API 终极版:

from django.shortcuts import render,HttpResponse
from django.http import JsonResponse
from app01 import models
from rest_framework import serializers
from django import views
from rest_framework.views import APIView
from rest_framework.response import Response
from app01 import serializers as app01_serializers
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin
from rest_framework.generics import GenericAPIView, RetrieveUpdateDestroyAPIView, ListCreateAPIView
from rest_framework.viewsets import ModelViewSet


from django.conf.urls import url
from app01 import views

urlpatterns = [
    # url(r"comment/$",views.Comment.as_view()),
    url(r"comment/(?P<pk>\d+)",views.CommentDetailView.as_view()),
    url(r"article/$",views.Article.as_view()),
    url(r"school/(?P<id>\d+)",views.SchoolDetail.as_view(),name="school-detail"),
    url(r"school_view/$",views.SchoolView.as_view(),name="school_view-detail"),
    url(r"comment_view/$",views.CommentView.as_view(),name="comment_view-detail"),
    url(r"article/(?P<pk>\d+)",views.ArticleDetail.as_view(),name="article-detail"),


    # url(r‘comment/$‘,views.CommentViewSet.as_view({
    #     "get":"list",
    #     "put":"create",
    # })),
    #
    # url(r‘comment/(?P<pk>\d+)/$‘,views.CommentViewSet.as_view({
    #     "get":"retrieve",
    #     "put":"update",
    #     "delete":"destroy"
    # }))

]

from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(rcomment,views.CommentViewSet)
urlpatterns += router.urls


#   相当于 在urlpatterns里添加了:
    # url(r‘comment/$‘,views.CommentViewSet.as_view({
        #     "get":"list",
        #     "put":"create",
        # })),
        #
        # url(r‘comment/(?P<pk>\d+)/$‘,views.CommentViewSet.as_view({
        #     "get":"retrieve",
        #     "put":"update",
        #     "delete":"destroy"
        # }))

 

RESTful API终极版序列化封装

标签:serial   view   delete   res   article   creat   get   rtc   import   

原文地址:https://www.cnblogs.com/stfei/p/9409865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!