1. 程式人生 > >django rest framework 解析器元件 介面設計,檢視元件 (1)

django rest framework 解析器元件 介面設計,檢視元件 (1)

一.解析器元件

-解析器元件是用來解析使用者請求資料的(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()),
urls
 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()
建立物件詳情

但是發現,使用這種會十分累,於是進行優化

檢視元件進行介面優化