一、序列化组件之HyperlinkedIdentityField
HyperlinkedIdentityField可以通过反向解析向前台返回一个链接
url(r'^Books/(?P<id>d+)', views.BooksDetail.as_view(),name='test'),
# 传三个参数
# view_name='test':路由名字,用来反向解析
# lookup_field='publish_id':要反向解析的参数值
# lookup_url_kwarg='id':有名分组的名字
url = serializers.HyperlinkedIdentityField(view_name='test', lookup_field='publish_id',lookup_url_kwarg='id')
class Books(APIView):
def get(self, request):
response = {'status': 200, 'msg': '查询成功', 'data': None}
books = models.Book.objects.all()
# context={'request':request}是必写的
ret = BooksSerializers(books, many=True,context={'request':request})
response['data'] = ret.data
return JsonResponse(response, safe=False)
# 返回结果
{
"status": 200,
"msg": "查询成功",
"data": [
{
"id": 1,
"url": "http://127.0.0.1:8000/Books/1",
"name": "红楼梦",
"price": "42.21",
"publish": 1,
"authors": [
1,
2
]
},
{
"id": 3,
"url": "http://127.0.0.1:8000/Books/2",
"name": "西游记",
"price": "12.32",
"publish": 2,
"authors": [
2
]
}
]
}
# url": "http://127.0.0.1:8000/Books/1"
# context={'request':request}:得到了域名http://127.0.0.1:8000
# view_name:得到了Books/(?P<id>d+)
# lookup_field 和lookup_url_kwarg:得到了1
# 把这三个拼接起来就成了一条路由http://127.0.0.1:8000/Books/1
二、序列化组件之数据校验
1.基本使用
class BooksSerializers(serializers.ModelSerializer):
class Meta:
model = models.Book
fields = '__all__'
# 添加数据
def post(self, request):
response = {'status': 200, 'msg': '添加成功', 'data': None}
# 反序列化把request.data中的JSON格式的数据传入data中
ret = BooksSerializers(data = request.data)
# is_valid 对反序列化后的数据进行校验
if ret.is_valid():
# 对校验成功的数据保存
ret.save()
response['data'] = ret.data
else:
response['status'] = 201
# errors:错误信息
response['data'] = ret.errors
response['msg'] = '添加失败'
return JsonResponse(response, safe=False)
# 如果是更新数据
def put(self, request, id):
response = {'status': 200, 'msg': '修改成功', 'data': None}
books = models.Book.objects.filter(pk=id).first()
if books:
# 不传instance,调save(),往数据库新增数据
# 传instance,调save(),修改数据
# BooksSerializers(data=request.data.instance='要更新的对象')
ret = BooksSerializers(data=request.data,instance=books)
if ret.is_valid():
ret.save()
response['data'] = ret.data
else:
response['status'] = 201
response['data'] = ret.errors
response['msg'] = '修改失败'
else:
response['status'] = 201
response['msg'] = '修改对象不存在'
return JsonResponse(response, safe=False)
2.自定义错误信息
class BooksSerializers(serializers.ModelSerializer):
class Meta:
model = models.Book
fields = '__all__'
# 类似forms组件
name = serializers.CharField(max_length=10, min_length=3, error_messages={'max_length': '最长为10','min_length': '最短为3','required':'不能为空'})
3.局部钩子以及全局钩子
# 局部钩子
# validate_字段名
def validate_name(self,value):
if value.startswith('sb'):
raise ValidationError('不能以sb开头')
else:
return value
# 全局钩子
# 只有通过字段校验才会判断全局钩子
def validate(self, value):
# value是通过校验的数据
print(value)
name = value.get('name')
price = value.get('price')
if name and price:
if str(name) == str(price):
# 通过判断返回value
return value
else:
# 没通过就报错
raise ValidationError('名字跟价格不相等')
return value
# 全局钩子error
# non_field_errors:[错误信息]