Restful Framework (三)
目录
一、版本
程序也来越大时,可能通过版本不同做不同的处理
没用rest_framework之前,我们可以通过以下这样的方式去获取。
class UserView(APIView):
def get(self,request,*args,**kwargs):
version = request.query_params.get('version')
print(version)
if version=='v1':
#如果版本是v1
ret = {
'code':111,
'msg':'版本一的内容'
} elif version=='v2':
# 如果是v2
ret = {
'code': 112,
'msg': '版本二的内容'
}
else:
ret = {
'code': 0,
'msg': '不支持其他版本'
}
return Response(ret)
现在我们来用rest_framework实现一下,有两种方式
1、基于url的方式
#基于url传参的形式
versioning_class = QueryParameterVersioning
#http://127.0.0.1:8080/api/users/?version=v2 #基于url的形式
versioning_class = URLPathVersioning
#http://127.0.0.1:8080/api/v1/users/
具体步骤
REST_FRAMEWORK = {
'DEFAULT_VERSION': 'v1', #默认的版本
'ALLOWED_VERSIONS': ['v1','v2'], #允许的版本
'VERSION_PARAM': 'version',
}
1、配置
from django.conf.urls import url,include
from django.contrib import admin urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/(?P<version>[v1|v2]+)/', include('api.urls'), name='users-list'),
]
urls.py
from api import views
urlpatterns = [
# url(r'^users/', views.UserView.as_view()),
url(r'^users/', views.UserView1.as_view()), ]
urls.py
class UserView1(APIView):
#基于url传参的形式
# versioning_class = QueryParameterVersioning
#http://127.0.0.1:8080/api/users/?version=v2 #基于url的形式
#http://127.0.0.1:8080/api/v1/users/
versioning_class = URLPathVersioning
def get(self,request,*args,**kwargs):
# self.dispatch
print(request.version) #打印的是版本
print(request.versioning_scheme) #打印的是对象
if request.version=='v2':
return Response('我是版本二')
elif request.version=='v1':
return Response('我是版本一')
else:
return Response('去去去')
views.py
注:在配置的时候
REST_FRAMEWORK = {
'VERSION_PARAM':'version',
'DEFAULT_VERSION':'v1',
'ALLOWED_VERSIONS':['v1','v2'],
# 'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.HostNameVersioning"
#如果加上这个配置就不用versioning_class = QueryParameterVersioning这样在指定了,
'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.URLPathVersioning" }
附加:restful提供的反向生成
#http://127.0.0.1:8080/api/v1/users/
#urls.py
#分发路由
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/(?P<version>[v1|v2]+)/', include('api.urls')),
] #api.urls.py
urlpatterns = [
url(r'^users/', views.UserView1.as_view(), name='users-list'),
] #views.py
导入类
from rest_framework.reverse import reverse
url = request.versioning_scheme.reverse(viewname='users-list',request=request)
print(url)
restfoamework反向解析
我们自己用django实现的,当前版本不一样的时候可以用这种方式
from django.urls import reverse
url = reverse(viewname='users-list',kwargs={'version':'v2'}) #指定的是v2就是v2,当你路径中输入v1的时候还是v2的路径
print(url) #/api/v2/users/
2、基于子域名传参
#分发url
urlpatterns = [
#url(r'^admin/', admin.site.urls),
url(r'^api/', include('api.urls')),
] urlpatterns = [
url(r'^users/', views.UsersView.as_view(),name='u'),
] class UsersView(APIView): def get(self,request,*args,**kwargs):
self.dispatch
print(request.version) # QueryParameterVersioning().detemiin_version()
print(request.versioning_scheme) # QueryParameterVersioning() REST_FRAMEWORK = {
'VERSION_PARAM':'version',
'DEFAULT_VERSION':'v1',
'ALLOWED_VERSIONS':['v1','v2'],
'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.HostNameVersioning"
} # C:\Windows\System32\drivers\etc
# vim /etc/hosts
127.0.0.1 v1.luffy.com
127.0.0.1 v2.luffy.com #配置ALLOWED_HOSTS = ['*']
基于子域名传参
如果遇到这样的错误
这是由于没有允许,解决办法,在settings里面配置一下
ALLOWED_HOSTS = ['*']
二、解析器:reqest.data取值的时候才执行
对请求的数据进行解析:是针对请求体进行解析的。表示服务器可以解析的数据格式的种类
django中的发送请求
#如果是这样的格式发送的数据,在POST里面有值
Content-Type: application/url-encoding.....
request.body
request.POST #如果是发送的json的格式,在POST里面是没有值的,在body里面有值,可通过decode,然后loads取值
Content-Type: application/json.....
request.body
request.POST
为了这种情况下每次都要decode,loads,显得麻烦,所以才有的解析器。弥补了django的缺点
客户端:
Content-Type: application/json
'{"name":"alex","age":123}' 服务端接收:
读取客户端发送的Content-Type的值 application/json parser_classes = [JSONParser,FormParser] #表示服务器可以解析的数据格式的种类
media_type_list = ['application/json','application/x-www-form-urlencoded'] 如果客户端的Content-Type的值和 application/json 匹配:JSONParser处理数据
如果客户端的Content-Type的值和 application/x-www-form-urlencoded 匹配:FormParser处理数据 配置:
单视图:
class UsersView(APIView):
parser_classes = [JSONParser,] 全局配置:
REST_FRAMEWORK = {
'VERSION_PARAM':'version',
'DEFAULT_VERSION':'v1',
'ALLOWED_VERSIONS':['v1','v2'],
# 'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.HostNameVersioning"
'DEFAULT_VERSIONING_CLASS':"rest_framework.versioning.URLPathVersioning",
'DEFAULT_PARSER_CLASSES':[
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.FormParser',
]
} class UserView(APIView):
def get(self,request,*args,**kwargs):
return Response('ok')
def post(self,request,*args,**kwargs):
print(request.data) #以后取值就在这里面去取值
return Response('...')
具体讲解
传上传文件
from django.conf.urls import url, include
from web.views import TestView urlpatterns = [
url(r'test/(?P<filename>[^/]+)', TestView.as_view(), name='test'),
]
urls.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.request import Request
from rest_framework.parsers import FileUploadParser class TestView(APIView):
parser_classes = [FileUploadParser, ] def post(self, request, filename, *args, **kwargs):
print(filename)
print(request.content_type) # 获取请求的值,并使用对应的JSONParser进行处理
print(request.data)
# application/x-www-form-urlencoded 或 multipart/form-data时,request.POST中才有值
print(request.POST)
print(request.FILES)
return Response('POST请求,响应内容') def put(self, request, *args, **kwargs):
return Response('PUT请求,响应内容')
views.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://127.0.0.1:8000/test/f1.numbers" method="post" enctype="multipart/form-data">
<input type="text" name="user" />
<input type="file" name="img"> <input type="submit" value="提交"> </form>
</body>
</html>
upload.html
全局使用
REST_FRAMEWORK = {
'DEFAULT_PARSER_CLASSES':[
'rest_framework.parsers.JSONParser'
'rest_framework.parsers.FormParser'
'rest_framework.parsers.MultiPartParser'
] }
settings.py
三、序列化
序列化用于对用户请求数据进行验证和数据进行序列化(为了解决queryset序列化问题)。
那什么是序列化呢?序列化就是把对象转换成字符串,反序列化就是把字符串转换成对象
models.py
from django.db import models # Create your models here.
class Group(models.Model):
title = models.CharField(max_length=32)
mu = models.ForeignKey(to='Menu',default=1) class UserInfo(models.Model):
name = models.CharField(max_length=32)
pwd = models.CharField(max_length=32)
group = models.ForeignKey(to="Group") roles = models.ManyToManyField(to="Role")
class Menu(models.Model):
name = models.CharField(max_length=21) class Role(models.Model):
name = models.CharField(max_length=32)
1、基本操作
from django.shortcuts import render,HttpResponse
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.versioning import BaseVersioning
from rest_framework.versioning import QueryParameterVersioning #获取version的值
from rest_framework.versioning import URLPathVersioning #支持版本
from rest_framework.versioning import HostNameVersioning
from rest_framework.parsers import JSONParser #解析器
from rest_framework import serializers
from app03 import models
class UsersSerializer(serializers.Serializer):
name = serializers.CharField() #字段名字
pwd = serializers.CharField() class UserView(APIView):
def get(self,request,*args,**kwargs):
# 方式一实现
# user_list = models.UserInfo.objects.values('name','pwd','group__mu','group__title')
# print(type(user_list))
# return Response(user_list) # 方式二之多对象
# user_list = models.UserInfo.objects.all() #直接这样查会报错,借助他提供的系列化
# ser = UsersSerializer(instance=user_list,many=True) #可允许多个
# # print(type(ser)) #<class 'rest_framework.serializers.ListSerializer'>
# print(ser.data) #返回的是一个有序字典 #方式三之单对象
user = models.UserInfo.objects.all().first()
ser = UsersSerializer(instance=user,many=False) return Response(ser.data)
views.py
2、跨表
x1 = serializers.CharField(source='group.mu.name')
如果你想跨表拿你任何需要的数据,都可以用上面的这种操作,内部做判断,如果可用内部就加括号调用了
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers
from app03 import models
class UsersSerializer(serializers.Serializer):
name = serializers.CharField() #字段名字
pwd = serializers.CharField()
# group = serializers.CharField() #会显示对象
# group_id = serializers.CharField() #会显示id
x1 = serializers.CharField(source='group.mu.name')
roles = serializers.CharField(source='roles.all') #多对多关系的这样查出的是queryset对象 class UserView2(APIView):
'''跨表操作'''
def get(self,request,*args,**kwargs): user = models.UserInfo.objects.all()
ser = UsersSerializer(instance=user,many=True) return Response(ser.data)
Views.py
3、复杂序列化
解决方案一:
class MyCharField(serializers.CharField): def to_representation(self, value): ##打印的是所有的数据
data_list = []
for row in value:
data_list.append(row.name)
return data_list class UsersSerializer(serializers.Serializer):
name = serializers.CharField() # obj.name
pwd = serializers.CharField() # obj.pwd
group_id = serializers.CharField() # obj.group_id
xxxx = serializers.CharField(source="group.title") # obj.group.title
x1 = serializers.CharField(source="group.mu.name") # obj.mu.name
# x2 = serializers.CharField(source="roles.all") # 多对多关系的这样查出的是queryset对象
x2 = MyCharField(source="roles.all") # obj.mu.name
Views.py
解决方案二:
class MyCharField(serializers.CharField):
def to_representation(self, value):
return {'id':value.pk, 'name':value.name} class UsersSerializer(serializers.Serializer):
name = serializers.CharField() # obj.name
pwd = serializers.CharField() # obj.pwd
group_id = serializers.CharField() # obj.group_id
xxxx = serializers.CharField(source="group.title") # obj.group.title
x1 = serializers.CharField(source="group.mu.name") # obj.mu.name
# x2 = serializers.CharField(source="roles.all") # obj.mu.name
x2 = serializers.ListField(child=MyCharField(),source="roles.all") # obj.mu.name
Views.py
解决方案三(推荐使用)
class UsersSerializer(serializers.Serializer):
name = serializers.CharField() # obj.name
pwd = serializers.CharField() # obj.pwd
group_id = serializers.CharField() # obj.group_id
xxxx = serializers.CharField(source="group.title") # obj.group.title
x1 = serializers.CharField(source="group.mu.name") # obj.mu.name
# x2 = serializers.CharField(source="roles.all") # obj.mu.name
# x2 = serializers.ListField(child=MyCharField(),source="roles.all") # obj.mu.name
x2 = serializers.SerializerMethodField() def get_x2(self,obj): #get_字段名
print(obj) ##UserInfo object
obj.roles.all()
role_list = obj.roles.filter(id__gt=1)
data_list = []
for row in role_list:
data_list.append({'pk':row.pk,'name':row.name})
return data_list
Views.py
4、基于Model
class UsersSerializer(serializers.ModelSerializer):
x1 = serializers.CharField(source='name')
group = serializers.HyperlinkedIdentityField(view_name='detail')
class Meta: model = models.UserInfo
# fields = "__all__"
fields = ['name','pwd','group','x1'] #自定义字段的时候注意要指定source,scource里面的数据必须是数据库有的数据
depth = 1 #表示深度 class UsersView(APIView):
def get(self,request,*args,**kwargs):
self.dispatch
# 方式一:
# user_list = models.UserInfo.objects.all().values('name','pwd','group__id',"group__title")
# return Response(user_list) # 方式二之多对象
user_list = models.UserInfo.objects.all()
# [obj1,obj2,obj3]
ser = UsersSerializer(instance=user_list,many=True)
return Response(ser.data)
Views.py
5、生成URL
class UsersSerializer(serializers.ModelSerializer): #
group = serializers.HyperlinkedIdentityField(view_name='detail')
class Meta:
model = models.UserInfo
fields = "__all__"
fields = ['name', 'pwd','group']
depth = 1 class UsersView(APIView):
def get(self,request,*args,**kwargs):
self.dispatch
# 方式一:
# user_list = models.UserInfo.objects.all().values('name','pwd','group__id',"group__title")
# return Response(user_list) # 方式二之多对象
user_list = models.UserInfo.objects.all()
# [obj1,obj2,obj3]
ser = UsersSerializer(instance=user_list,many=True,context={'request':request})
return Response(ser.data)
views.py
from django.conf.urls import url,include
from django.contrib import admin
from app03 import views
urlpatterns = [ url(r'^users4/', views.UserView4.as_view(), name='xxx'), #吧users4的group的值反向生成users5的url
url(r'^users5/(?P<pk>.*)', views.UserView5.as_view(), name='detail'), #必须叫pk
# url(r'^users4/(?P<pk>.*)', views.UserView4.as_view(), name='detail'),
]
6、全局生成URL
class UsersSerializer(serializers.HyperlinkedModelSerializer): #继承他自动生成
class Meta:
model = models.UserInfo
fields = "__all__" # fields = ['id','name','pwd'] class UsersView(APIView):
def get(self,request,*args,**kwargs):
self.dispatch
# 方式一:
# user_list = models.UserInfo.objects.all().values('name','pwd','group__id',"group__title")
# return Response(user_list) # 方式二之多对象
user_list = models.UserInfo.objects.all()
# [obj1,obj2,obj3]
ser = UsersSerializer(instance=user_list,many=True,context={'request':request})
return Response(ser.data)
views.py
四、请求数据验证:
a、自己手写
class PasswordValidator(object):
def __init__(self, base):
self.base = base def __call__(self, value):
if value != self.base:
message = '用户输入的值必须是 %s.' % self.base
raise serializers.ValidationError(message) def set_context(self, serializer_field):
"""
This hook is called by the serializer instance,
prior to the validation call being made.
"""
# 执行验证之前调用,serializer_fields是当前字段对象
pass class UsersSerializer(serializers.Serializer):
name = serializers.CharField(min_length=6)
pwd = serializers.CharField(error_messages={'required': '密码不能为空'}, validators=[PasswordValidator('')])
views.py
b、基于model
class PasswordValidator(object):
def __init__(self, base):
self.base = base def __call__(self, value):
if value != self.base:
message = '用户输入的值必须是 %s.' % self.base
raise serializers.ValidationError(message) def set_context(self, serializer_field):
"""
This hook is called by the serializer instance,
prior to the validation call being made.
"""
# 执行验证之前调用,serializer_fields是当前字段对象
pass class UsersSerializer(serializers.ModelSerializer):
class Meta:
model = models.UserInfo
fields = "__all__"
#自定义验证规则
extra_kwargs = {
'name': {'min_length': 6},
'pwd': {'validators': [PasswordValidator(666), ]}
}
views.py
使用
class UsersView(APIView):
def get(self,request,*args,**kwargs):
self.dispatch
# 方式一:
# user_list = models.UserInfo.objects.all().values('name','pwd','group__id',"group__title")
# return Response(user_list) # 方式二之多对象
user_list = models.UserInfo.objects.all()
# [obj1,obj2,obj3]
ser = UsersSerializer(instance=user_list,many=True,context={'request':request})
return Response(ser.data) def post(self,request,*args,**kwargs):
ser = UsersSerializer(data=request.data)
if ser.is_valid():
print(ser.validated_data)
else:
print(ser.errors)
return Response('...')
viewS.py
钩子函数
def validate_字段(self,validated_value):
raise ValidationError(detail='xxxxxx')
return validated_value
Restful Framework (三)的更多相关文章
- 4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍
4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍 天涯明月笙 关注 2018.02.20 19:23* 字数 762 阅读 135 ...
- 3- vue django restful framework 打造生鲜超市 - model设计和资源导入
3- vue django restful framework 打造生鲜超市 - model设计和资源导入 使用Python3.6与Django2.0.2(Django-rest-framework) ...
- django restful framework 一对多方向更新数据库
目录 django restful framework 序列化 一 . 数据模型: models 二. 序列化: serializers 三, 视图: views 四, 路由: urls 五. 测试 ...
- Springboot & Mybatis 构建restful 服务三
Springboot & Mybatis 构建restful 服务三 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务二 2 restful ...
- 在django restful framework中设置django model的property
众所周知,在django的model中,可以某些字段设置@property和setter deleter getter,这样就可以在存入数据的时候进行一些操作,具体原理请参见廖雪峰大神的博客https ...
- Restful framework【第三篇】序列化组件
基本使用 -序列化 -对象,转成json格式 用drf的序列化组件 -定义一个类继承class BookSerializer(serializers.Serializer): -写字段,如果不指定so ...
- Restful Framework 初识
目录 一.什么是RESTful 二.什么是API 三.RESTful API规范 四.基于Django实现API 五.基于Django Rest Framework框架实现 一. 什么是RESTful ...
- django restful framework教程大全
一. 什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角 ...
- Restful framework【第六篇】认证组件
基本用法 -认证功能 1 写一个类,继承BaseAuthentication 2 def authenticate(self,request) ,记住传request对象 -如果验证通过,返回None ...
随机推荐
- Linux系统上的popen()库函数
popen可以是系统命令,也可以是自己写的程序a.out. 假如a.out就是打印 “hello world“ 在代码中,想获取什么,都可以通过popen获取. 比如获取ls的信息, 比如获取自己写的 ...
- Consul 入门(二)
KV 存储 通过命令行操作 $ consul kv put hello world # 设置数据 Success! Data written to: hello $ consul kv get hel ...
- ufw坑
ufw就是一个iptables的快捷应用.今天被这个给坑了. 一个同时没事随便修改ufw,结果ssh登陆不上,ldap什么的都被阻断了. 直接iptables -F,结果忘了修改policy,直接没法 ...
- Elastic Search操作入门
前言 Elastic Search是基于Lucene这个非常成熟的索引方案,另加上一些分布式的实现:集群,sharding,replication等.具体可以参考我同事写的文章. 本文主要介绍ES入门 ...
- 2017 JAVA神器 Btrace详细介绍
官网:https://github.com/btraceio/btrace 下载:https://github.com/btraceio/btrace/releases/tag/v1.3.9 文档:h ...
- 【bzoj】1927 [Sdoi2010]星际竞速
[算法]最小费用最大流 [题解]跟滑雪略有类似,同样因为可以重复所以不是最小路径覆盖. 连向汇的边容量为1足矣,因为一个点只会出去一次(路径结束). bzoj 1927 [Sdoi2010]星际竞速 ...
- js_如何优化你的代码让它更好看
1.对于美的东西我们很难拒绝,比如美女.哈哈哈,程序员的梗. 2.所以我希望我写出来的代码也是很美观的,让人看起来会很舒服. 3.要想让你的代码简约美观,就要涉及封装,模块化了,可复用代码.vue可以 ...
- ifa_local 和 ifa_address
ifa_local 和 ifa_address区别联系: 1. 在配置了支持广播的接口上,与IFA_LOCAL一样,同样表示本地ip地址: 2. 对于点对点链路,IFA_ADDRESS表示的是对端的地 ...
- python基础===python os.path模块
os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...
- python基础===codecs打开文件,解决文件编码格式的问题
codecs https://docs.python.org/3/library/codecs.html 我们经常用open打开文件的时候会出现各式各样的错误,编码格式的问题,等等~真的很烦 现在尽量 ...