加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Django Rest Framework(版本、解析器、序列化、数据验证)

发布时间:2020-12-15 17:20:41 所属栏目:大数据 来源:网络整理
导读:div id="cnblogs_post_body" class="blogpost-body" 一、版本 程序也来越大时,可能通过版本不同做不同的处理 没用rest_framework之前,我们可以通过以下这样的方式去获

<div id="cnblogs_post_body" class="blogpost-body">

一、版本

程序也来越大时,可能通过版本不同做不同的处理

没用rest_framework之前,我们可以通过以下这样的方式去获取。

get(self,request,*args,** version = request.query_params.get( version== ret = :111 : version== ret = : 112 : ret = : Response(ret)

现在我们来用rest_framework实现一下

a. 基于url的get传参方式

如:/users?version=v1

REST_FRAMEWORK =: , : [,], : }

django.conf.urls web.views urlpatterns =<span style="color: #000000;"> [
url(r
<span style="color: #800000;">'
<span style="color: #800000;">^test/
<span style="color: #800000;">'
,TestView.as_view(),name=<span style="color: #800000;">'
<span style="color: #800000;">test<span style="color: #800000;">'<span style="color: #000000;">),]

rest_framework.views rest_framework.response rest_framework.versioning <span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
versioning_class
=<span style="color: #000000;"> QueryParameterVersioning

</span><span style="color: #0000ff;"&gt;def</span> get(self,**<span style="color: #000000;"&gt;kwargs):

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 获取版本</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.version)
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 获取版本管理的类</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.versioning_scheme)

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 反向生成URL</span>
    reverse_url = request.versioning_scheme.reverse(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;test</span><span style="color: #800000;"&gt;'</span>,request=<span style="color: #000000;"&gt;request)
    </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(reverse_url)

    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;GET请求,响应内容</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;)

</span><span style="color: #0000ff;"&gt;def</span> post(self,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;POST请求,响应内容</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;)

</span><span style="color: #0000ff;"&gt;def</span> put(self,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;PUT请求,响应内容</span><span style="color: #800000;"&gt;'</span>)</pre>

b. 基于url的正则方式

如:/v1/users/

REST_FRAMEWORK =: , : }

django.conf.urls web.views urlpatterns =<span style="color: #000000;"> [
url(r
<span style="color: #800000;">'
<span style="color: #800000;">^(?P[v1|v2]+)/test/
<span style="color: #800000;">'
,]

rest_framework.views rest_framework.response rest_framework.versioning <span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
versioning_class
=<span style="color: #000000;"> URLPathVersioning

</span><span style="color: #0000ff;"&gt;def</span> get(self,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 获取版本</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.version)
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 获取版本管理的类</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.versioning_scheme)

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 反向生成URL</span>
    reverse_url = request.versioning_scheme.reverse(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;test</span><span style="color: #800000;"&gt;'</span>,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;PUT请求,响应内容</span><span style="color: #800000;"&gt;'</span>)</pre>

c. 基于accept 请求头方式

如:Accept: application/json; version=1.0

REST_FRAMEWORK =: , : }

django.conf.urls

rest_framework.views rest_framework.response rest_framework.versioning <span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
versioning_class
=<span style="color: #000000;"> AcceptHeaderVersioning

</span><span style="color: #0000ff;"&gt;def</span> get(self,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 获取版本 HTTP_ACCEPT头</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.version)
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 获取版本管理的类</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.versioning_scheme)
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 反向生成URL</span>
    reverse_url = request.versioning_scheme.reverse(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;test</span><span style="color: #800000;"&gt;'</span>,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;PUT请求,响应内容</span><span style="color: #800000;"&gt;'</span>)</pre>

d. 基于主机名方法

如:v1.example.com

ALLOWED_HOSTS = [=: , : }

django.conf.urls

rest_framework.views rest_framework.response rest_framework.versioning <span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
versioning_class
=<span style="color: #000000;"> HostNameVersioning

</span><span style="color: #0000ff;"&gt;def</span> get(self,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 获取版本</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.version)
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 获取版本管理的类</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.versioning_scheme)
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 反向生成URL</span>
    reverse_url = request.versioning_scheme.reverse(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;test</span><span style="color: #800000;"&gt;'</span>,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;PUT请求,响应内容</span><span style="color: #800000;"&gt;'</span>)</pre>

<div class="cnblogs_code" onclick="cnblogs_code_show('c8d351b2-3a89-442f-8af5-5996311a9bb9')">


<div id="cnblogs_code_open_c8d351b2-3a89-442f-8af5-5996311a9bb9" class="cnblogs_code_hide">


            urlpatterns =
                url(r,include(        urlpatterns </span>=<span style="color: #000000;"&gt; [
            url(r</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;^users/</span><span style="color: #800000;"&gt;'</span>,views.UsersView.as_view(),name=<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;u</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;),]


        </span><span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt; UsersView(APIView):

            </span><span style="color: #0000ff;"&gt;def</span> get(self,**<span style="color: #000000;"&gt;kwargs):
                self.dispatch
                </span><span style="color: #0000ff;"&gt;print</span>(request.version) <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; QueryParameterVersioning().detemiin_version()</span>
                <span style="color: #0000ff;"&gt;print</span>(request.versioning_scheme) <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; QueryParameterVersioning()</span>

<span style="color: #000000;">

        REST_FRAMEWORK </span>=<span style="color: #000000;"&gt; {
            </span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;VERSION_PARAM</span><span style="color: #800000;"&gt;'</span>:<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;version</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;DEFAULT_VERSION</span><span style="color: #800000;"&gt;'</span>:<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;v1</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;ALLOWED_VERSIONS</span><span style="color: #800000;"&gt;'</span>:[<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;v1</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;v2</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;],</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;DEFAULT_VERSIONING_CLASS</span><span style="color: #800000;"&gt;'</span>:<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;rest_framework.versioning.HostNameVersioning</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;
        }

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; C:WindowsSystem32driversetc</span>
        <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; vim /etc/hosts</span>
        127.0.0.1<span style="color: #000000;"&gt;    v1.luffy.com
        </span>127.0.0.1<span style="color: #000000;"&gt;    v2.luffy.com

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;配置ALLOWED_HOSTS = ['*']</span></pre>

如果遇到这样的错误

ALLOWED_HOSTS = []

e. 基于django路由系统的namespace

如:example.com/v1/users/

REST_FRAMEWORK =: , : }

django.conf.urls web.views urlpatterns =<span style="color: #000000;"> [
url(r
<span style="color: #800000;">'
<span style="color: #800000;">^v1/
<span style="color: #800000;">'
<span style="color: #000000;">,([
url(r
<span style="color: #800000;">'<span style="color: #800000;">test/<span style="color: #800000;">',],None,<span style="color: #800000;">'<span style="color: #800000;">v1<span style="color: #800000;">'<span style="color: #000000;">)),url(r<span style="color: #800000;">'<span style="color: #800000;">^v2/<span style="color: #800000;">'<span style="color: #000000;">,<span style="color: #800000;">'<span style="color: #800000;">v2<span style="color: #800000;">'<span style="color: #000000;">)),]

rest_framework.views rest_framework.response rest_framework.versioning <span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
versioning_class
=<span style="color: #000000;"> NamespaceVersioning

</span><span style="color: #0000ff;"&gt;def</span> get(self,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;PUT请求,响应内容</span><span style="color: #800000;"&gt;'</span>)</pre>

#http://127.0.0.1:8080/api/v1/users/

urlpatterns =[v1|v2]+)/,]

<span style="color: #008000;">#<span style="color: #008000;">api.urls.py
urlpatterns =<span style="color: #000000;"> [
url(r<span style="color: #800000;">'<span style="color: #800000;">^users/<span style="color: #800000;">',views.UserView1.as_view(),name=<span style="color: #800000;">'<span style="color: #800000;">users-list<span style="color: #800000;">'<span style="color: #000000;">),]

<span style="color: #008000;">#<span style="color: #008000;">views.py
<span style="color: #000000;">导入类
<span style="color: #0000ff;">from rest_framework.reverse <span style="color: #0000ff;">import<span style="color: #000000;"> reverse
url = request.versioning_scheme.reverse(viewname=<span style="color: #800000;">'<span style="color: #800000;">users-list<span style="color: #800000;">',request=<span style="color: #000000;">request)
<span style="color: #0000ff;">print(url)

我们自己用django实现的,当前版本不一样的时候可以用这种方式

django.urls = reverse(viewname=,kwargs={:}) (url)

f. 全局使用

REST_FRAMEWORK =:: : [,:

注:在配置的时候

REST_FRAMEWORK =:           #如果加上这个配置就不用versioning_class = QueryParameterVersioning这样再指定了,
   :

<h1 class="title">二、解析器(parser):reqest.data取值的时候才执行

对请求的数据进行解析:是针对请求体进行解析的。表示服务器可以解析的数据格式的种类

django中的发送请求

Content-Type: application/url-<span style="color: #008000;">#<span style="color: #008000;">如果是发送的json的格式,在POST里面是没有值的,在body里面有值,可通过decode,然后loads取值
Content-Type: application/<span style="color: #000000;">json.....
request.body
request.POST

为了这种情况下每次都要decode,loads,显得麻烦,所以才有的解析器。弥补了django的缺点

Content-Type: application/ 读取客户端发送的Content-Type的值 application/ parser_classes = [JSONParser,FormParser] media_type_list = [, 如果客户端的Content-Type的值和 application/ 如果客户端的Content-Type的值和 application/x-www-form- parser_classes = REST_FRAMEWORK = : : :[, : get(self,** Response( post(self,** (request.data) Response()

根据请求头 content-type 选择对应的解析器就请求体内容进行处理。

a. 仅处理请求头content-type为application/json的请求体

django.conf.urls web.views.s5_parser urlpatterns =<span style="color: #000000;"> [
url(r
<span style="color: #800000;">'
<span style="color: #800000;">test/
<span style="color: #800000;">'
,]

rest_framework.views rest_framework.response rest_framework.request rest_framework.parsers <span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
parser_classes
=<span style="color: #000000;"> [JSONParser,]

</span><span style="color: #0000ff;"&gt;def</span> post(self,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.content_type)

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 获取请求的值,并使用对应的JSONParser进行处理</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.data)

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.POST)
    </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.FILES)

    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;POST请求,响应内容</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;)

</span><span style="color: #0000ff;"&gt;def</span> put(self,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;PUT请求,响应内容</span><span style="color: #800000;"&gt;'</span>)</pre>

b.仅处理请求头content-type为application/x-www-form-urlencoded的请求体

django.conf.urls web.views urlpatterns =<span style="color: #000000;"> [
url(r
<span style="color: #800000;">'
<span style="color: #800000;">test/
<span style="color: #800000;">'
,]

rest_framework.views rest_framework.response rest_framework.request rest_framework.parsers <span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
parser_classes
=<span style="color: #000000;"> [FormParser,**<span style="color: #000000;">kwargs):
<span style="color: #0000ff;">return
Response(<span style="color: #800000;">'
<span style="color: #800000;">PUT请求,响应内容
<span style="color: #800000;">'
)

c.仅处理请求头content-type为multipart/form-data的请求体

django.conf.urls

rest_framework.views rest_framework.response rest_framework.request rest_framework.parsers <span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
parser_classes
=<span style="color: #000000;"> [MultiPartParser,**<span style="color: #000000;">kwargs):
<span style="color: #0000ff;">print
<span style="color: #000000;">(request.content_type)

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 获取请求的值,并使用对应的JSONParser进行处理</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.data)
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.POST)
    </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.FILES)
    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;POST请求,响应内容</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;)

</span><span style="color: #0000ff;"&gt;def</span> put(self,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;PUT请求,响应内容</span><span style="color: #800000;"&gt;'</span>)</pre>

> > Title
method= enctype=> name= /> name=>
<input type=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;submit</span><span style="color: #800000;"&gt;"</span> value=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;提交</span><span style="color: #800000;"&gt;"</span>>

d. 仅上传文件

django.conf.urls web.views urlpatterns = url(r[^/]+), ]

rest_framework.views rest_framework.response rest_framework.request rest_framework.parsers parser_classes = post(self,filename,** Response( put(self,** Response()

> > Title
method= enctype=> name= /> name=> value=>

e. 同时多个Parser

当同时使用多个parser时,rest framework会根据请求头content-type自动进行比对,并使用对应parser

django.conf.urls

rest_framework.views rest_framework.response rest_framework.request rest_framework.parsers <span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
parser_classes
=<span style="color: #000000;"> [JSONParser,MultiPartParser,**<span style="color: #000000;">kwargs):
<span style="color: #0000ff;">return
Response(<span style="color: #800000;">'
<span style="color: #800000;">PUT请求,响应内容
<span style="color: #800000;">'
)

f.全局使用

REST_FRAMEWORK = }

django.conf.urls

rest_framework.views rest_framework.response <span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
<span style="color: #0000ff;">def
post(self,**<span style="color: #000000;">kwargs):
<span style="color: #0000ff;">return
Response(<span style="color: #800000;">'
<span style="color: #800000;">PUT请求,响应内容
<span style="color: #800000;">'
)

request.data取POST的值

注意:个别特殊的值可以通过Django的request对象 request._request 来进行获取

获取get的数据,可以通过request._request.GET或者request.query_params或request.GET

因为

<div class="cnblogs_code" onclick="cnblogs_code_show('5d2e387b-236c-4b61-bdfa-8b780f067cb2')">


<div id="cnblogs_code_open_5d2e387b-236c-4b61-bdfa-8b780f067cb2" class="cnblogs_code_hide">

     dispatch(self,**==
        request = self.initialize_request(request,**= request

(self,parsers=None,authenticators==None,parser_context= .,request.. self._request </span>= request <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;将django中的request对象封装到了_request中</span></pre>

getattr(self._request,attr) self.(attr)

<h1 id="lable3" class="title">三、序列化

序列化用于对用户请求数据进行验证和数据进行序列化(为了解决queryset序列化问题)。

那什么是序列化呢?序列化就是把对象转换成字符串,反序列化就是把字符串转换成对象

<p class="secondtitle">a. 自定义字段


<div class="cnblogs_code" onclick="cnblogs_code_show('e5624de1-596d-43b9-8998-b8788167d828')">


<div id="cnblogs_code_open_e5624de1-596d-43b9-8998-b8788167d828" class="cnblogs_code_hide">

 django.conf.urls  web.views.s6_serializers urlpatterns =<span style="color: #000000;"> [
url(r
<span style="color: #800000;">'
<span style="color: #800000;">test/
<span style="color: #800000;">'
,]

rest_framework.views rest_framework.response rest_framework .. <span style="color: #0000ff;">class<span style="color: #000000;"> PasswordValidator(object):
<span style="color: #0000ff;">def
<span style="color: #800080;">init
<span style="color: #000000;">(self,base):
self.base
=<span style="color: #000000;"> base

</span><span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__call__</span><span style="color: #000000;"&gt;(self,value):
    </span><span style="color: #0000ff;"&gt;if</span> value !=<span style="color: #000000;"&gt; self.base:
        message </span>= <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;This field must be %s.</span><span style="color: #800000;"&gt;'</span> %<span style="color: #000000;"&gt; self.base
        </span><span style="color: #0000ff;"&gt;raise</span><span style="color: #000000;"&gt; serializers.ValidationError(message)

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; set_context(self,serializer_field):
    </span><span style="color: #800000;"&gt;"""</span><span style="color: #800000;"&gt;
    This hook is called by the serializer instance,prior to the validation call being made.
    </span><span style="color: #800000;"&gt;"""</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 执行验证之前调用,serializer_fields是当前字段对象</span>
    <span style="color: #0000ff;"&gt;pass</span>

<span style="color: #0000ff;">class<span style="color: #000000;"> UserSerializer(serializers.Serializer):
ut_title = serializers.CharField(source=<span style="color: #800000;">'<span style="color: #800000;">ut.title<span style="color: #800000;">'<span style="color: #000000;">)
user = serializers.CharField(min_length=6<span style="color: #000000;">)
pwd = serializers.CharField(error_messages={<span style="color: #800000;">'<span style="color: #800000;">required<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">密码不能为空<span style="color: #800000;">'},validators=[PasswordValidator(<span style="color: #800000;">'<span style="color: #800000;">666<span style="color: #800000;">'<span style="color: #000000;">)])

<span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
<span style="color: #0000ff;">def get(self,**<span style="color: #000000;">kwargs):

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 序列化,将数据库查询字段序列化为字典</span>
    data_list =<span style="color: #000000;"&gt; models.UserInfo.objects.all()
    ser </span>= UserSerializer(instance=data_list,many=<span style="color: #000000;"&gt;True)
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 或</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; obj = models.UserInfo.objects.all().first()</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; ser = UserSerializer(instance=obj,many=False)</span>
    <span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; Response(ser.data)

</span><span style="color: #0000ff;"&gt;def</span> post(self,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 验证,对请求发来的数据进行验证</span>
    ser = UserSerializer(data=<span style="color: #000000;"&gt;request.data)
    </span><span style="color: #0000ff;"&gt;if</span><span style="color: #000000;"&gt; ser.is_valid():
        </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(ser.validated_data)
    </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt;:
        </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(ser.errors)

    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;POST请求,响应内容</span><span style="color: #800000;"&gt;'</span>)</pre>

b. 基于Model自动生成字段

django.conf.urls

rest_framework.views rest_framework.response rest_framework .. <span style="color: #0000ff;">class<span style="color: #000000;"> PasswordValidator(object):
<span style="color: #0000ff;">def
<span style="color: #800080;">init
<span style="color: #000000;">(self,base):
self.base
=<span style="color: #000000;"> str(base)

</span><span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__call__</span><span style="color: #000000;"&gt;(self,serializer_fields是当前字段对象</span>
    <span style="color: #0000ff;"&gt;pass</span>

<span style="color: #0000ff;">class<span style="color: #000000;"> ModelUserSerializer(serializers.ModelSerializer):

user </span>= serializers.CharField(max_length=32<span style="color: #000000;"&gt;)

</span><span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt; Meta:
    model </span>=<span style="color: #000000;"&gt; models.UserInfo
    fields </span>= <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;__all__</span><span style="color: #800000;"&gt;"</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; fields = ['user','pwd','ut']</span>
    depth = 2<span style="color: #000000;"&gt;
    extra_kwargs </span>= {<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;user</span><span style="color: #800000;"&gt;'</span>: {<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;min_length</span><span style="color: #800000;"&gt;'</span>: 6},<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;pwd</span><span style="color: #800000;"&gt;'</span>: {<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;validators</span><span style="color: #800000;"&gt;'</span>: [PasswordValidator(666<span style="color: #000000;"&gt;),]}}
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; read_only_fields = ['user']</span>

<span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
<span style="color: #0000ff;">def get(self,**<span style="color: #000000;">kwargs):

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 序列化,将数据库查询字段序列化为字典</span>
    data_list =<span style="color: #000000;"&gt; models.UserInfo.objects.all()
    ser </span>= ModelUserSerializer(instance=data_list,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 验证,对请求发来的数据进行验证</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.data)
    ser </span>= ModelUserSerializer(data=<span style="color: #000000;"&gt;request.data)
    </span><span style="color: #0000ff;"&gt;if</span><span style="color: #000000;"&gt; ser.is_valid():
        </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(ser.validated_data)
    </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt;:
        </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(ser.errors)

    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;POST请求,响应内容</span><span style="color: #800000;"&gt;'</span>)</pre>

c. 生成URL

django.conf.urls d+)/,name=

rest_framework.views rest_framework.response rest_framework .. <span style="color: #0000ff;">class<span style="color: #000000;"> PasswordValidator(object):
<span style="color: #0000ff;">def
<span style="color: #800080;">init
<span style="color: #000000;">(self,serializer_fields是当前字段对象

<span style="color: #0000ff;">pass

<span style="color: #0000ff;">class<span style="color: #000000;"> ModelUserSerializer(serializers.ModelSerializer):
ut = serializers.HyperlinkedIdentityField(view_name=<span style="color: #800000;">'<span style="color: #800000;">detail<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">class<span style="color: #000000;"> Meta:
model =<span style="color: #000000;"> models.UserInfo
fields = <span style="color: #800000;">"<span style="color: #800000;">all<span style="color: #800000;">"<span style="color: #000000;">

    extra_kwargs </span>=<span style="color: #000000;"&gt; {
        </span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;user</span><span style="color: #800000;"&gt;'</span>: {<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;min_length</span><span style="color: #800000;"&gt;'</span>: 6<span style="color: #000000;"&gt;},</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;pwd</span><span style="color: #800000;"&gt;'</span>: {<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;validators</span><span style="color: #800000;"&gt;'</span>: [PasswordValidator(666<span style="color: #000000;"&gt;),]},}

<span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
<span style="color: #0000ff;">def get(self,many=True,context={<span style="color: #800000;">'<span style="color: #800000;">request<span style="color: #800000;">'<span style="color: #000000;">: request})
<span style="color: #008000;">#<span style="color: #008000;"> 或
<span style="color: #008000;">#<span style="color: #008000;"> obj = models.UserInfo.objects.all().first()
<span style="color: #008000;">#<span style="color: #008000;"> ser = UserSerializer(instance=obj,**<span style="color: #000000;">kwargs):
<span style="color: #008000;">#<span style="color: #008000;"> 验证,对请求发来的数据进行验证
<span style="color: #0000ff;">print<span style="color: #000000;">(request.data)
ser = ModelUserSerializer(data=<span style="color: #000000;">request.data)
<span style="color: #0000ff;">if<span style="color: #000000;"> ser.is_valid():
<span style="color: #0000ff;">print<span style="color: #000000;">(ser.validated_data)
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">print<span style="color: #000000;">(ser.errors)

    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;POST请求,响应内容</span><span style="color: #800000;"&gt;'</span>)</pre>

d. 自动生成URL

django.conf.urls

rest_framework.views rest_framework.response rest_framework .. <span style="color: #0000ff;">class<span style="color: #000000;"> PasswordValidator(object):
<span style="color: #0000ff;">def
<span style="color: #800080;">init
<span style="color: #000000;">(self,serializer_fields是当前字段对象

<span style="color: #0000ff;">pass

<span style="color: #0000ff;">class<span style="color: #000000;"> ModelUserSerializer(serializers.HyperlinkedModelSerializer):
ll = serializers.HyperlinkedIdentityField(view_name=<span style="color: #800000;">'<span style="color: #800000;">xxxx<span style="color: #800000;">'<span style="color: #000000;">)
tt = serializers.CharField(required=<span style="color: #000000;">False)

</span><span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt; Meta:
    model </span>=<span style="color: #000000;"&gt; models.UserInfo
    fields </span>= <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;__all__</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;
    list_serializer_class </span>=<span style="color: #000000;"&gt; serializers.ListSerializer

    extra_kwargs </span>=<span style="color: #000000;"&gt; {
        </span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;user</span><span style="color: #800000;"&gt;'</span>: {<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;min_length</span><span style="color: #800000;"&gt;'</span>: 6<span style="color: #000000;"&gt;},</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;url</span><span style="color: #800000;"&gt;'</span>: {<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;view_name</span><span style="color: #800000;"&gt;'</span>: <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;xxxx</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;},</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;ut</span><span style="color: #800000;"&gt;'</span>: {<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;view_name</span><span style="color: #800000;"&gt;'</span>: <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;xxxx</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;},}

<span style="color: #0000ff;">class<span style="color: #000000;"> TestView(APIView):
<span style="color: #0000ff;">def get(self,<span style="color: #000000;">kwargs):
<span style="color: #008000;">#<span style="color: #008000;"> # 序列化,将数据库查询字段序列化为字典
data_list =<span style="color: #000000;"> models.UserInfo.objects.all()
ser = ModelUserSerializer(instance=data_list,context={<span style="color: #800000;">'<span style="color: #800000;">request<span style="color: #800000;">'<span style="color: #000000;">: request})
<span style="color: #008000;">#<span style="color: #008000;"> # 如果Many=True
<span style="color: #008000;">#<span style="color: #008000;"> # 或
<span style="color: #008000;">#<span style="color: #008000;"> # obj = models.UserInfo.objects.all().first()
<span style="color: #008000;">#<span style="color: #008000;"> # ser = UserSerializer(instance=obj,
<span style="color: #000000;">kwargs):
<span style="color: #008000;">#<span style="color: #008000;"> 验证,对请求发来的数据进行验证
<span style="color: #0000ff;">print<span style="color: #000000;">(request.data)
ser = ModelUserSerializer(data=<span style="color: #000000;">request.data)
<span style="color: #0000ff;">if<span style="color: #000000;"> ser.is_valid():
<span style="color: #0000ff;">print<span style="color: #000000;">(ser.validated_data)
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">print<span style="color: #000000;">(ser.errors)

    </span><span style="color: #0000ff;"&gt;return</span> Response(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;POST请求,响应内容</span><span style="color: #800000;"&gt;'</span>)</pre>

<h2 class="secondtitle">1、基本操作
<div class="cnblogs_code" onclick="cnblogs_code_show('9ac5e44c-4c0c-4a38-abb5-166b39368ee8')">


<div id="cnblogs_code_open_9ac5e44c-4c0c-4a38-abb5-166b39368ee8" class="cnblogs_code_hide">

 django.db <span style="color: #008000;">#<span style="color: #008000;"> Create your models here.
<span style="color: #0000ff;">class<span style="color: #000000;"> Group(models.Model):
title = models.CharField(max_length=32<span style="color: #000000;">)
mu = models.ForeignKey(to=<span style="color: #800000;">'<span style="color: #800000;">Menu<span style="color: #800000;">',default=1<span style="color: #000000;">)

<span style="color: #0000ff;">class<span style="color: #000000;"> UserInfo(models.Model):
name = models.CharField(max_length=32<span style="color: #000000;">)
pwd = models.CharField(max_length=32<span style="color: #000000;">)
group = models.ForeignKey(to=<span style="color: #800000;">"<span style="color: #800000;">Group<span style="color: #800000;">"<span style="color: #000000;">)

roles </span>= models.ManyToManyField(to=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;Role</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)

<span style="color: #0000ff;">class<span style="color: #000000;"> Menu(models.Model):
name = models.CharField(max_length=21<span style="color: #000000;">)

<span style="color: #0000ff;">class<span style="color: #000000;"> Role(models.Model):
name = models.CharField(max_length=32)

<div class="cnblogs_code" onclick="cnblogs_code_show('2921de3f-601d-4995-8679-14a8261f74cf')">


<div id="cnblogs_code_open_2921de3f-601d-4995-8679-14a8261f74cf" class="cnblogs_code_hide">

  django.shortcuts   rest_framework.views   rest_framework.response   rest_framework.versioning   rest_framework.versioning  QueryParameterVersioning  
  rest_framework.versioning  URLPathVersioning 
  rest_framework.versioning   rest_framework.parsers  JSONParser  
  rest_framework   app03       name = serializers.CharField()  
     pwd = 
       get(self,**         
         
         
         
 
         
         
         
         
         
 
         
         user =         ser = UsersSerializer(instance=user,many= 
          Response(ser.data)

x1 = serializers.CharField(source=)

如果你想跨表拿你任何需要的数据,都可以用上面的这种操作,内部做判断,如果可用内部就加括号调用了

rest_framework.views rest_framework.response rest_framework app03 name = serializers.CharField() pwd = x1 = serializers.CharField(source= roles = serializers.CharField(source=) get(self,** user = ser = UsersSerializer(instance=user,many= Response(ser.data)

自定义类和方法

解决方案一:

to_representation(self,value): data_list = row name = serializers.CharField() pwd = serializers.CharField() group_id = serializers.CharField() xxxx = serializers.CharField(source=) x1 = serializers.CharField(source=) x2 = MyCharField(source=)

解决方案二:

{:value.pk, name = serializers.CharField() pwd = serializers.CharField() group_id = serializers.CharField() xxxx = serializers.CharField(source=) x1 = serializers.CharField(source=) x2 = serializers.ListField(child=MyCharField(),source=)

解决方案三(推荐使用)

name = serializers.CharField() pwd = serializers.CharField() group_id = serializers.CharField() xxxx = serializers.CharField(source=) x1 = serializers.CharField(source=) x2 = get_x2(self,obj): (obj) role_list = obj.roles.filter(id__gt=1 data_list = row data_list.append({:row.pk,

= serializers.CharField(source== serializers.HyperlinkedIdentityField(view_name= model </span>=<span style="color: #000000;"&gt; models.UserInfo </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; fields = "__all__"</span> fields = [<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;name</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;pwd</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;group</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;x1</span><span style="color: #800000;"&gt;'</span>] <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;自定义字段的时候注意要指定source,scource里面的数据必须是数据库有的数据</span> depth = 1 <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;表示深度</span>

<span style="color: #0000ff;">class<span style="color: #000000;"> UsersView(APIView):
<span style="color: #0000ff;">def get(self,**<span style="color: #000000;">kwargs):
self.dispatch
<span style="color: #008000;">#<span style="color: #008000;"> 方式一:
<span style="color: #008000;">#<span style="color: #008000;"> user_list = models.UserInfo.objects.all().values('name','groupid',"grouptitle")
<span style="color: #008000;">#<span style="color: #008000;"> return Response(user_list)

    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 方式二之多对象</span>
    user_list =<span style="color: #000000;"&gt; models.UserInfo.objects.all()
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; [obj1,obj2,obj3]</span>
    ser = UsersSerializer(instance=user_list,many=<span style="color: #000000;"&gt;True)
    </span><span style="color: #0000ff;"&gt;return</span> Response(ser.data)</pre>

=</span></span><span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt; Meta: model </span>=<span style="color: #000000;"&gt; models.Article fields </span>= [<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;title</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;source</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;brief</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;view_num</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;comment_num</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;collect_num</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;published_time</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;head_img</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;content</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;agree_num</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;] <span style="color: #008000;"&gt;# 将自定义的字段放到fields中,通过fields='__all__'也可以


depth = 1 <span style="color: #008000;">#<span style="color: #008000;"> 表示深度

<span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; get_published_time(self,obj):  <span style="color: #008000;"&gt;# 定义方法,def get_自定义的字段名:</span>
    time </span>= (now() -<span style="color: #000000;"&gt; obj.pub_date).days

    week,day </span>= divmod(time,7<span style="color: #000000;"&gt;)
    hour,min </span>= divmod(int((now() - obj.pub_date).seconds),3600<span style="color: #000000;"&gt;)
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; print(week)</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; print(day)</span>
    <span style="color: #0000ff;"&gt;if</span> week > 1<span style="color: #000000;"&gt;:
        </span><span style="color: #0000ff;"&gt;return</span> <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;%s周前</span><span style="color: #800000;"&gt;'</span> %<span style="color: #000000;"&gt; week
    </span><span style="color: #0000ff;"&gt;elif</span> day > 1<span style="color: #000000;"&gt;:
        </span><span style="color: #0000ff;"&gt;return</span> <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;%s天前</span><span style="color: #800000;"&gt;'</span> %<span style="color: #000000;"&gt; day
    </span><span style="color: #0000ff;"&gt;elif</span> hour > 1<span style="color: #000000;"&gt;:
        </span><span style="color: #0000ff;"&gt;return</span> <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;%s小时前</span><span style="color: #800000;"&gt;'</span> %<span style="color: #000000;"&gt; hour
    </span><span style="color: #0000ff;"&gt;elif</span> min > 1<span style="color: #000000;"&gt;:
        </span><span style="color: #0000ff;"&gt;return</span> <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;%s小时前</span><span style="color: #800000;"&gt;'</span> %<span style="color: #000000;"&gt; min
    </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt;:
        </span><span style="color: #0000ff;"&gt;return</span> <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;刚刚</span><span style="color: #800000;"&gt;'</span></pre>

<h2 class="secondtitle">5、指定生成URL


<div class="cnblogs_code" onclick="cnblogs_code_show('f9318372-7b65-48b4-9a34-82e9f64dd158')">


<div id="cnblogs_code_open_f9318372-7b65-48b4-9a34-82e9f64dd158" class="cnblogs_code_hide">

 UsersSerializer(serializers.ModelSerializer):      group = serializers.HyperlinkedIdentityField(view_name=)   
    == 
        
        depth = 1

<span style="color: #0000ff;">class<span style="color: #000000;"> UsersView(APIView):
<span style="color: #0000ff;">def get(self,context={<span style="color: #800000;">'<span style="color: #800000;">request<span style="color: #800000;">':request}) <span style="color: #008000;">#<span style="color: #008000;">要反向生成url时,必须带上这个context,否则报错
<span style="color: #0000ff;">return Response(ser.data)

django.conf.urls django.contrib app03 =url(r</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;^users4/</span><span style="color: #800000;"&gt;'</span>,views.UserView4.as_view(),name=<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;xxx</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;),url(r</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;^users5/(?P<pk>.*)</span><span style="color: #800000;"&gt;'</span>,views.UserView5.as_view(),name=<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;detail</span><span style="color: #800000;"&gt;'</span>),<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;必须叫pk</span>

]

UsersSerializer(serializers.HyperlinkedModelSerializer): model = fields = get(self,** user_list = ser = UsersSerializer(instance=user_list,context={ Response(ser.data)

self.base = value != message = % name = serializers.CharField(min_length=6 pwd = serializers.CharField(error_messages={: },validators=[PasswordValidator()])

model = fields = extra_kwargs = : {: 6 : {: [PasswordValidator(666 }

使用

get(self,** user_list = ser = UsersSerializer(instance=user_list,context={ post(self,** ser = UsersSerializer(data= Response()

钩子函数

ValidationError(detail=

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读