标签:from add _id self 字段 lte creat ros django
URL:
urlpatterns = [
url(r‘^admin/‘, admin.site.urls),
url(r‘^publi/‘, views.LoginView.as_view()),
url(r‘^publisher/‘, views.PublishView.as_view()),
url(r‘^books/$‘, views.BookView.as_view()),
url(r‘^books/(\d+)/‘, views.BookDetailView.as_view()),
url(r‘^author/$‘, views.AuthorView.as_view()),
url(r‘^author/(\d+)/‘, views.AuthorDetailView.as_view()),
]
views:
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()
rest_framework中的serializers,Response,APIView
标签:from add _id self 字段 lte creat ros django
原文地址:https://www.cnblogs.com/chvv/p/9631794.html