rest_framework中的serializers,Response,APIView

2021-07-11 17:08

阅读:1952

from django.shortcuts import render,HttpResponse

# Create your views here.

from .models import *
from django.views import View
import json
from rest_framework import serializers
from rest_framework.response import Response
#为queyset做序列化
class PublisherSerializers(serializers.Serializer):
name = serializers.CharField()
email = serializers.CharField()

# class BookSerializers(serializers.Serializer):
# title = serializers.CharField()
# price = serializers.IntegerField()
# pub_date = serializers.DateField()
# publisher = serializers.CharField(source="publisher.email")
# # authors = serializers.CharField(source="authors.all") #多对多不好用
# authors =serializers.SerializerMethodField()
# def get_authors(self,obj):
# temp = []
# for obj in obj.authors.all():
# temp.append(obj.name)
# print(temp)
# return temp
class BookModelSerializers(serializers.ModelSerializer):
class Meta:
model =Book
fields = "__all__"
#自定义显示字段 可以不不加就显示全部 用自定义的话post请求要重写create方法
publisher = serializers.CharField(source="publisher.pk",read_only=True)
# authors = serializers.CharField(source="authors.all") #多对多不好用
# authors =serializers.SerializerMethodField()
# def get_authors(self,obj):
# temp = []
# for obj in obj.authors.all():
# temp.append(obj.name)
# print(temp)
# return temp

def create(self, validated_data):
print("validated_data",validated_data)
book = Book.objects.create(title=validated_data["title"],price=validated_data["price"],pub_date=validated_data["pub_date"],publisher_id=validated_data["publisher"]["pk"])
book.authors.add(*validated_data["authors"])
return book

class AuthorModelSerializers(serializers.ModelSerializer):
class Meta:
model = Author
fields = "__all__"

from rest_framework.views import APIView
class PublishView(APIView):
def get(self,request):
print(request.GET)
return HttpResponse(123)
def post(self,request):
print(request.body)
print(request.POST)

return HttpResponse("789")
class BookView(APIView):
def get(self,request):
book_list = Book.objects.all()
# bs = BookSerializers(book_list,many=True)
bs = BookModelSerializers(book_list, many=True)
print(bs.data)
return Response(bs.data)
def post(self,request):
bs = BookModelSerializers(data=request.data)
if bs.is_valid():
bs.save()
return Response(bs.data)
else:
return Response(bs.errors)
class BookDetailView(APIView):
def get(self,request,id):
book = Book.objects.filter(pk=id).first()
bs = BookModelSerializers(book)
return Response(bs.data)
def put(self,request,id):
book = Book.objects.filter(pk=id).first()
bs = BookModelSerializers(book,data=request.data)
if bs.is_valid():
bs.save()
return Response(bs.data)
else:
return Response(bs.errors)
def delete(self,request,id):
Book.objects.filter(pk=id).delete()
return Response()
class AuthorView(APIView):
def get(self,request):
author_list = Author.objects.all()
authorseria = AuthorModelSerializers(author_list,many=True)
return Response(authorseria.data)
def post(self,request):
authorseria = AuthorModelSerializers(data=request.data)
if authorseria.is_valid():
authorseria.save()
return Response(authorseria.data)
else:
return Response(authorseria.errors)
class AuthorDetailView(APIView):
def get(self,request,pk):
author_obj = Author.objects.filter(pk=pk).first()
author_seria = AuthorModelSerializers(author_obj)
return Response(author_seria.data)
def put(self,request,pk):
author_obj = Author.objects.filter(pk=pk).first()
author_seria = AuthorModelSerializers(author_obj,data=request.data)
if author_seria.is_valid():
author_seria.save()
return Response(author_seria.data)
else:
return Response(author_seria.errors)

def delete(self,request,pk):
Author.objects.filter(pk=pk).first().delete()
return Response()

上一篇:Win10安装Redis

下一篇:flex部局 API


评论


亲,登录后才可以留言!