day100-序列化组件-LMLPHP

3 序列化组件

class BookSerializer(serializers.Serializer):

    title = serializers.CharField( max_length=32)
price =serializers.DecimalField(max_digits=5,decimal_places=2) # 一对多字段
publish_email=serializers.CharField(max_length=32,source="publish.email")
publish_name=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):
ret=[]
for i in obj.authors.all():
ret.append({"name":i.name,"age":i.name})
return ret ''' serializer=BookSerializer(book_list,many=True)
serializer.data: temp=[]
for obj in book_list:
temp.append({
"title":obj.title,
"price":obj.price,
"publish":obj.publish.email,
if 字段SerializerMethodField:
"authors":get_authors(obj)
}) json.dumps(temp) ''' class BookSerializer(serializers.ModelSerializer):
class Meta:
model=Book
# fields=["title","price"]
fields="__all__" class BookView(APIView): def get(self,request):
book_list=Book.objects.all()
serializer=BookSerializer(book_list,many=True) return Response(serializer.data) def post(self,request): print(request.data) serializer=BookSerializer(data=request.data,many=False) if serializer.is_valid():
serializer.save() # create操作 return Response(serializer.data) # 序列化提交数据
else:
return Response(serializer.errors) # 序列化错误信息 今日内容: 1 序列化组件 序列化: queryset------>[{},{},{}]
obj ------>{} 2 视图组 class AuthorsView(ModelViewSet):
queryset = Author.objects.all()
serializer_class = AuthorSerializer #url(r'^authors/$', views.AuthorsView.as_view({"get":"list","post":"create"})),
#url(r'^authors/$', ViewSetMixin.as_view({"get":"list","post":"create"})),
#url(r'^authors/$', ViewSetMixin.view),
一旦用户访问,get访问authors/:
ViewSetMixin.view(reqeust):
for method, action in actions.items(): # {"get":"list","post":"create"}
handler = getattr(self, action) # handler===self.list
setattr(self, method, handler) # self.get=self.list return self.dispatch(request, *args, **kwargs)
#
APIView.dispatch(request): handler = getattr(self, request.method.lower()) # self.list
response = handler(request, *args, **kwargs)
# def list(self.request):
` # return ..... return response ################### #url(r'^authors/(?P<pk>\d+)/', views.AuthorsView.as_view({"get":"retrieve","delete":"destroy","put":"update"})),
#url(r'^authors/$', ViewSetMixin.as_view({"get":"retrieve","delete":"destroy","put":"update"})),
#url(r'^authors/$', ViewSetMixin.view),
一旦用户访问,get访问authors/1:
ViewSetMixin.view(reqeust):
for method, action in actions.items(): # {"get":"retrieve","delete":"destroy","put":"update"}
handler = getattr(self, action) # handler===self.retrieve handler=== destroy handler===update
setattr(self, method, handler) # self.get=self.retrieve self.delete=self.destroy self.put=self.update return self.dispatch(request, *args, **kwargs)
#
APIView.dispatch(request): handler = getattr(self, request.method.lower()) # self.retrieve
response = handler(request, *args, **kwargs)
# def retrieve(self.request):
` # return ..... return response
05-08 15:39