RESTful API终极版序列化封装

Posted Qingqiu_Gu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RESTful API终极版序列化封装相关的知识,希望对你有一定的参考价值。

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终极版序列化封装的主要内容,如果未能解决你的问题,请参考以下文章

Feign 接入第三方restful api 入门

JS版汉字与拼音互转终极方案,附简单的JS拼音

html 基本终极版代码

PHP MYSQL 生成JSON代码 终极版

RESTful API - 批量操作的分块响应

高级Java程序员必问,Redis事务终极篇