RESTful API终极版序列化封装

2021-03-25 14:25

阅读:437

标签: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\d+)",views.CommentDetailView.as_view()),
    url(r"article/$",views.Article.as_view()),
    url(r"school/(?P\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\d+)",views.ArticleDetail.as_view(),name="article-detail"),


    # url(r‘comment/$‘,views.CommentViewSet.as_view({
    #     "get":"list",
    #     "put":"create",
    # })),
    #
    # url(r‘comment/(?P\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\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\d+)",views.CommentDetailView.as_view()),
    url(r"article/$",views.Article.as_view()),
    url(r"school/(?P\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\d+)",views.ArticleDetail.as_view(),name="article-detail"),


    # url(r‘comment/$‘,views.CommentViewSet.as_view({
    #     "get":"list",
    #     "put":"create",
    # })),
    #
    # url(r‘comment/(?P\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\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


评论


亲,登录后才可以留言!