restframework CBV试图的4种方式

day100



上节回顾

1 APIView流程

2 序列化组件(restframework)

1 # 自定义序列化组件
class BookSerializers(serializers.Serializer):
title=serializers.CharField()
price=serializers.CharField()
publishDate=serializers.DateField()

# 针对一对多
publish=serializers.CharField(source="publish.name")
publish_email=serializers.CharField(source="publish.email")
# 针对多对多
authors=serializers.SerializerMethodField()
def get_authors(self,obj):
data=[]
for i in obj.authors.all():
temp=[]
temp.append(i.pk)
temp.append(i.name)
data.append(temp)
return data
# ModelSerializer序列化组件
class BookSerializers(serializers.ModelSerializer):
class Meta:
model=Book
fields="__all__"

publish = serializers.CharField(source="publish.name")
authors=serializers.SerializerMethodField()
def get_authors(self,obj):
data=[]
for i in obj.authors.all():
temp=[]
temp.append(i.pk)
temp.append(i.name)
data.append(temp)
return data


2 5个接口

"books" GET:获取所有数据
POST:添加一条数据

"book/1" GET:获取单条数据
DELETE:删除一条数据
PUT/PATCH:更新一条数据


3
(1) 功能之序列化:
book_list = Book.objects.all()
ps = BookSerializers(book_list, many=True)
ps.data

book_obj=Book.objects.filter(pk=pk).first()
bs=BookSerializers(book_obj,many=False)

(2) 校验字段与操作记录(ModelSerializer拥有)
# 添加数据
bs=BookSerializers(data=request.data)
if bs.is_valid(): #校验字段接口
bs.save() # 生成记录接口
return Response(bs.data) # 序列化接口


# 更新数据
bs=BookSerializers(data=request.data,instance=book_obj)
if bs.is_valid(): #校验字段接口
bs.save() #更新记录接口
return Response(bs.data) # 序列化接口


3 视图

(1)

class AuthorView(APIView):
def get():
pass
def post():
pass

class AuthorDetailView(APIView):
def get():
pass
def delete():
pass
def put():
pass

(2) class AuthorView(CreateModelMixin,ListModelMixin,generics.GenericAPIView,):

queryset=Author.objects.all
serializer_class=AuthorSerializers

def get(self,request):
return self.list(request)
def post(self,request):

return self.create(request)

class AuthorDetailView(DestroyModelMixin,UpdateModelMixin,RetrieveModelMixin,generics.GenericAPIView,):
queryset = Author.objects.all
serializer_class = AuthorSerializers

def get(self,request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)

def put(self,request, *args, **kwargs):
return self.update(request, *args, **kwargs)

def delete(self,request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
(3) class AuthorView(generics.ListCreateAPIView):

queryset=Author.objects.all
serializer_class=AuthorSerializers

class AuthorDetailView(generics.RetrieveUpdateDestroyAPIView):
queryset = Author.objects.all
serializer_class = AuthorSerializers


(4) class AuthorModelView(ModelViewSet):

queryset=Author.objects.all()
serializer_class=AuthorSerializers

ViewSetMixIn


    第4种的url要传参数,注意 如:

      url(r'^publishs/$',views.PublishView.as_view({"get":"list","post":"create"})),
  
url(r'^publishs/(?P<pk>\d+)/$',views.PublishView.as_view({"put":"update","get":"retrieve","delete":"destroy"}))


4 解析器
class APIView:
parser_classes = api_settings.DEFAULT_PARSER_CLASSES


posted @ 2018-08-02 16:22  赵伟航  阅读(247)  评论(0编辑  收藏  举报