day 95 RestFramework序列化組件
阿新 • • 發佈:2018-06-21
ram creat render res port pri import LV pla
一 、復習
1. CBV流程
class BookView(View): def get(): pass def post(): pass #url(r‘^books/‘, views.BookView.as_view()), #url(r‘^books/‘, View.as_view()), #url(r‘^books/‘, View.view), 一旦用戶訪問books: View.view(request)==View.dispatch(request)==self.get(request)
2.Restframework下的APIView流程
class BookView(APIView(View)): def get(): pass def post(): pass #url(r‘^books/‘, views.BookView.as_view()), #url(r‘^books/‘, APIView.as_view()), #url(r‘^books/‘, View.view), 一旦用戶訪問books: View.view(request)==APIView.dispatch(request)====self.get(request) 3 新的request對象 def APIView.dispatch(request): request = self.initialize_request(request, *args, **kwargs) request._request.GET request._request.POST request.POST request.GET request.data(針對post請求,put/patch)
3.序列化組件
from rest_framework import serializers class BookSerializers(serializers.Serializer): title=serializers.CharField(max_length=32) price=serializers.DecimalField(max_digits=8,decimal_places=2) publish=serializers.CharField(max_length=32,source="publish.name") #authors=serializers.CharField(max_length=32,source="authors.all") authors=serializers.SerializerMethodField() def get_authors(self,obj): temp=[] for obj in obj.authors.all(): temp.append(obj.name) return temp ‘‘‘ bs=BookSerializers(book_list,many=True) temp=[] for obj in book_list: temp.append({ ‘title‘:obj.title, ‘price‘:obj.price, ‘publish‘:obj.publish.name, ‘authors‘: }) bs.data=json.dumps(temp) ‘‘‘ class BookView(APIView): def get(self,request): book_list=Book.objects.all() bs=BookSerializers(book_list,many=True) # [{},{},{}] bs1=BookSerializers(model_obj) # {} return Response(bs1.data) def post(self, request): print("request.data",request.data) print("request.data",request.data["title"]) print("request.POST",request.POST) return HttpResponse("post.....")
二 、 RestFramwork之序列化組件1(get請求) .
from django.shortcuts import render,HttpResponse,redirect # Create your views here. from django.views import View from app01.models import Book import json from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import serializers """ 有了modelSerializer後以下的代碼都沒用啦 。 from rest_framework import serializers class BookSerializers(serializers.Serializer): title =serializers.CharField(max_length=32) price =serializers.DecimalField(max_digits=8,decimal_places=2) publish =serializers.CharField(max_length=32,source="publish.Email") # authors =serializers.CharField(max_length=32,source="authors.all") authors =serializers.SerializerMethodField() def get_authors(self,obj): temp =[] for obj in obj.authors.all(): temp.append(obj.name) return temp """ """ #總結的源碼 bs =BookSerializers(book_list,many=True) temp=[] for obj in book_list: temp.append({ ’titlte‘:obj.title, ’price‘:obj.price, ’publish‘:obj.publish.name, ‘authors‘:obj.authors. }) #總結的源碼 bs.data =json.dumps(temp) """ from rest_framework.serializers import ModelSerializer class BookModelSerializers(ModelSerializer): class Meta: model =Book fileds ="__all__" authors =serializers.SerializerMethodField() def get_authors(self,obj): temp =[] for obj in obj.authors.all(): temp.append(obj.name) return temp class BookView(APIView): """ get請求:獲取所有的書籍 post請求:添加一本書籍 """ def get(self,request): book_list =Book.objects.all() bs =BookModelSerializers(book_list,many= True) return Response(bs.data) """ def get(self,request): print(request._request.GET) print(request.GET) print(request.query_params) book_list= Book.objects.all() # 方式一: temp=[] from django.forms.models import model_to_dict # for book in book_list: # temp.append(model_to_dict(book)) #序列化方式一 django提供的. #方式二: # from django.core.serializers import serialize # data =serialize("json",book_list) #序列化方式二 django提供的. # return Response(data) #方式三: bs =BookSerializers(book_list,many=True) print("bs:is",bs) return Response(bs.data) """ class BookDetailView(View): pass
以上代碼有問題打印不出來 publish 和author字段的內容.
三、RestFramwork之序列化組件2(POST請求).
day 95 RestFramework序列化組件