标签:自己实现 详情 出版社 验证 设计 view ide read_only 书籍
一.解析器组件
-解析器组件是用来解析用户请求数据的(application/json), content-type 将客户端发来的json数据进行解析
-必须适应APIView
-request.data触发
二.序列化组件
2.1.django 自带组件serializer
2.1.1 from django.serializers import serialize
2.1.2 origin_data = Book.objects.all()
2.1.3 serialized_data = serialize("json", origin_data)
2.2 DRF的序列化组件
-get接口设计 本质上是基于解释器进行json数据解析的组件 接口就是url 存储的唯一资源
使用:
2.2.1 from rest_framework import serializers
2.2.2 创建一个序列化类
class BookSerializer(serializers.Serializer):
publish_name = serializers.CharField(read_only=True, source="publish.name")
authors_list = serializers.SerializerMethodField()
def get_authors_list(self, book_obj):
pass
2.2.3 开始序列化
origin_data = Book.objects.all()
serialized_data = BookSerializer(origin_data, many=True)
return Response(serialized_data.data)
- post接口设计
总结:
1. serializers.Serializer无法插入数据,只能自己实现create
2. 字段太多,不能自动序列化
- get单条数据接口设计
1. 定义url
2. 获取数据对象
3. 开始序列化:serialized_data = BookSerializer(book_obj, many=False)
4. 返回数据:serialized_data.data
- delete
- put
1. 定义url
2. 获取数据对象
2.1 book_obj = Book.objects.get(pk=1)
3. 开始序列化(验证数据,save())
2.2 verified_data = BookSerializer(instance=book_obj, many=False)
4. 验证成功写入数据库,验证失败返回错误
4.1 verified_data.is_valid()
1 #re_path(r‘books/$‘, views.BookView.as_view()), 2 #re_path(r‘books/(?P<pk>\d+)/$‘, views.BookFilterView.as_view()),
1 ‘‘‘ 2 class BookSerializer(serializers.Serializer): 3 # 这里字段一定要与models的一致 4 title = serializers.CharField(max_length=32) 5 price = serializers.DecimalField(max_digits=5, decimal_places=2) 6 # 一对多 具体告诉返回什么而不是对象,这里返回的是一个出版社的城市 7 publish = serializers.CharField(max_length=32, source=‘publish.city‘) 8 # 多对多 需要手动查找 9 authors_list = serializers.SerializerMethodField() 10 # get_ + authors_list 11 def get_authors_list(self, book_obj): 12 author_list = list() 13 # book_obj 是每一个书籍对象,通过和关联字段找到所有名字 14 15 for author in book_obj.authors.all(): 16 author_list.append(author.name) 17 # 最后通过get_ 返回 18 return author_list 19 # 自定义post 20 def create(self, book_obj): 21 book_obj["publish_id"] = book_obj.pop("publish") 22 book = Book.objects.create(**book_obj) 23 return book 24 # 这样发现这个在字段多的表是 25 ‘‘‘
1 # 2 # class BookView(APIView): 3 # def get(self,request): 4 # course_data=Book.objects.all() 5 # book_data =BookSerializer(course_data,many=True) 6 # 7 # return Response(book_data.data) 8 # # 笨方法 9 # def post(self,request): 10 # verified_data = BookSerializer(data=request.data) 11 # print("verified_data",verified_data) 12 # if verified_data.is_valid(): 13 # book =verified_data.save() 14 # print("request.data",request.data) 15 # authors = Author.objects.filter(nid__in=request.data[‘author_list‘]) 16 # print("authors",authors) 17 # book.authors.add(*authors) 18 # return Response(verified_data.data) 19 # class BookFilterView(APIView): 20 # def get(self,request,nid): 21 # book_obj = Book.objects.get(pk=nid) 22 # serialized_data=BookSerializer(book_obj,many=False) 23 # return Response(serialized_data.data) 24 # 25 # def put(self,request,nid): 26 # book_obj = Book.objects.get(pk=nid) 27 # verified_data = BookSerializer(data=request.data,instance=book_obj) 28 # if verified_data.is_valid(): 29 # verified_data.save() 30 # return Response(verified_data.data) 31 # else: 32 # return Response(verified_data.errors) 33 # 34 # def delete(self,request,nid): 35 # book_obj = Book.objects.get(pk=nid).delete() 36 # return Response()
但是发现,使用这种会十分累,于是进行优化
视图组件进行接口优化
django rest framework 解析器组件 接口设计,视图组件 (1)
标签:自己实现 详情 出版社 验证 设计 view ide read_only 书籍
原文地址:https://www.cnblogs.com/zhangqing979797/p/10084249.html