django之drf框架(两个视图基类、5个扩展视图类、9个视图子类)
两个视图基类
APIView和GenericAPIView
drf提供的最顶层的父类就是APIView,以后所有的类都继承自他
GenericAPIView继承自APIView,他里面封装了一些工能
基于APIView+ModelSerializer+Resposne写5个接口
子路由:app01>>>urls.py
from django.urls import path, include
from . import views
urlpatterns = [
path('books/', views.BookView.as_view()),
path('books/<int:pk>', views.BookDetailView.as_view()),
path('publish/', views.PublishView.as_view()),
path('publish/<int:pk>', views.PublishDetailView.as_view()),
]
主路由:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include('app01.urls')),
]
序列化类:
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ['id', 'name', 'price', 'publish', 'authors', 'publish_detail', 'author_list']
extra_kwargs = {
'publish': {'write_only': True},
'authors': {'write_only': True},
'publish_detail': {'read_only': True},
'author_list': {'read_only': True}
}
模型表:
from django.db import models
class Book(models.Model):
name = models.CharField(max_length=32)
price = models.DecimalField(max_digits=5, decimal_places=2)
publish = models.ForeignKey(to='Publish', on_delete=models.CASCADE)
authors = models.ManyToManyField(to='Author')
@property
def publish_detail(self):
return {'name': self.publish.name, 'city': self.publish.city}
@property
def author_list(self):
l = []
for author in self.authors.all():
l.append({'name': author.name, 'age': author.age})
return l
def __str__(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField()
author_detail = models.OneToOneField(to='AuthorDetail', on_delete=models.CASCADE)
def __str__(self):
return self.name
class AuthorDetail(models.Model):
telephone = models.BigIntegerField()
birthday = models.DateField()
addr = models.CharField(max_length=64)
class Publish(models.Model):
name = models.CharField(max_length=32)
city = models.CharField(max_length=32)
email = models.EmailField()
def __str__(self):
return self.name
class Meta:
verbose_name = '出版社'
verbose_name_plural = verbose_name
视图类:
# 第一层:继承APIView+序列化类+Response写接口
class BookView(APIView):
def get(self, request):
book_list = Book.objects.all()
ser = BookSerializer(instance=book_list, many=True)
return Response(ser.data)
def post(self, request):
ser = BookSerializer(data=request.data)
if ser.is_valid():
ser.save()
return Response(ser.data)
else:
return Response(ser.errors)
class BookDetailView(APIView):
def put(self,request,*args,**kwargs):
book = Book.objects.filter(pk=kwargs.get('pk')).first()
ser = BookSerializer(instance=book,data=request.data)
if ser.is_valid():
ser.save()
return Response(ser.data)
else:
return Response(ser.errors)
def get(self,request,*args,**kwargs):
book = Book.objects.filter(pk=kwargs.get('pk')).first()
ser = BookSerializer(instance=book)
return Response(ser.data)
def delete(self,request,*args,**kwargs):
Book.objects.filter(pk=kwargs.get('pk')).delete()
return Response('')
继承GenericAPIView,编写五个接口
class BookView(GenericAPIView):
# 配置两个类属性
queryset = Book.objects.all()
serializer_class = BookSerializer
def get(self, request):
obj_list = self.get_queryset()
ser = self.get_serializer(instance=obj_list, many=True)
return Response(ser.data)
def post(self, request):
ser = self.get_serializer(data=request.data)
if ser.is_valid():
ser.save()
return Response(ser.data)
else:
return Response(ser.errors)
class BookDetailView(GenericAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
def put(self, request, *args, **kwargs):
# book = Book.objects.filter(pk=kwargs.get('pk')).first()
obj = self.get_object() # 获取单条数据
ser = self.get_serializer(instance=obj, data=request.data)
if ser.is_valid():
ser.save()
return Response(ser.data)
else:
return Response(ser.errors)
def get(self, request, *args, **kwargs):
obj = self.get_object()
ser = self.get_serializer(instance=obj)
return Response(ser.data)
def delete(self, request, *args, **kwargs):
self.get_object().delete()
return Response('')
继承GenericAPIView+序列化类+Response写接口
#1 继承GenericAPIView的写法
-1 在类中,写两个类属性:所有数据,序列化类
queryset = Book.objects.all()
serializer_class = BookSerializer
-2 获取所有要序列化的数据
self.get_queryset()
-3 获取序列化类
self.get_serializer(参数跟之前一样)
-4 获取单挑
self.get_object()
# 2 如果想快速写出Publish的5个接口,只需要修改视图类上的两个类属性即可,其他的不用动
queryset = Publish.objects.all()
serializer_class = PublishSerializer
# GenericAPIView源码分析
-1 继承了APIView
-2 有些类属性--》目前只记住两个queryset,serializer_class
queryset # 要序列化的所有数据
serializer_class # 序列化类
lookup_field = 'pk' # 查询单条,前端传入的参数对应值【pk】,转换器
filter_backends # 后续要学的,过滤
pagination_class # 后续要学的,分页
-3 有些对象方法
-get_queryset: 返回待序列化的数据
1 调用 .all
2 在子类中重写,控制要序列化的数据
-get_serializer: 返回 序列化类 以后用它
-本质就是---》 self.serializer_class(instance=object_list, many=True)
-内部调用了:self.get_serializer_class
-后期在子类中重写get_serializer_class,返回什么序列化类,以后就以哪个序列化类做序列化
- get_serializer_class 它是用来重写的
def get_serializer_class(self):
if self.request.method=='GET':
return '序列化的类'
else:
return '反序列化的类'
-get_object 获取单条---》根据它:lookup_field 获取
五个视图扩展类
继承GenericAPIView+5个视图扩展类+序列化类+Response
# 第三层:继承GenericAPIView+5个视图扩展类+序列化类+Response
from rest_framework.mixins import CreateModelMixin, ListModelMixin, RetrieveModelMixin, UpdateModelMixin,DestroyModelMixin
class BookView(GenericAPIView, CreateModelMixin, ListModelMixin):
# 配置两个类属性
queryset = Book.objects.all()
serializer_class = BookSerializer
def get(self, request):
return super().list(request)
def post(self, request):
return super().create(request)
class BookDetailView(GenericAPIView, RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin):
queryset = Book.objects.all()
serializer_class = BookSerializer
def put(self, request, *args, **kwargs):
return super(BookDetailView, self).update(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
return super().retrieve(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return super().destroy(request, *args, **kwargs)
九个视图子类
这九个是视图子类,不需要额外继承GenericAPIView,只需要继承9个中其中某个,就会有某个或某几个接口,可以点击ListAPIView源码分析可知,该视图类有一个get方法,返回是一个list;该类继承的是GenericAPIView,父类有的方法,子类都可以使用,所以不再需要额外继承GenericAPIView
""" 基于上面再封装成九个视图类"""
from rest_framework.generics import ListAPIView, CreateAPIView, ListCreateAPIView
from rest_framework.generics import RetrieveAPIView, UpdateAPIView, DestroyAPIView, RetrieveDestroyAPIView, \
RetrieveUpdateAPIView, RetrieveUpdateDestroyAPIView # 没有DestroyUpdateAPIView方法,前提是没有查询删除不了
# 查询图书所有
class BookListView(ListAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
# 图书新增
class BookCreateView(CreateAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
# 图书修改
class BookUpdateView(UpdateAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
# 查询图书单条
class BookRetrieveView(RetrieveAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
# 删除图书
class BookDestroyView(DestroyAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
# 图书查询和删除功能
class BookRetrieveDestroyView(RetrieveDestroyAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
# 图书查询和修改功能
class BookRetrieveUpdateView(RetrieveUpdateAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
# 图书查询、修改和删除
# 查询图书单条
class BookRetrieveUpdateDestroyView(RetrieveUpdateDestroyAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
""" 总结:
ListAPIView + CreateAPIView = ListCreateAPIView 给BookView继承的
RetrieveAPIView + DestroyAPIView = RetrieveDestroyAPIView 给BookDetailView视图提供的
RetrieveAPIView + UpdateAPIView = RetrieveDestroyAPIView 给BookDetailView视图提供的
RetrieveAPIView + UpdateAPIView + DestroyAPIView = RetrieveUpdateDestroyAPIView 给BookDetailView视图提供的
发现Destroy 和Update 方法没有整合,
"""
最终通过继承可以写成下面两个类
""" 将上述的功能再整合一起"""
# 路由
urlpatterns = [
path('books/', views.BookView.as_view()),
path('books/<int:pk>/', views.BookView.as_view()),
]
# 视图
class BookAPIView(ListCreateAPIView):# 查询所有和新增一个
queryset = Book.objects.all()
serializer_class = BookSerializer
class BookDetailView(RetrieveUpdateDestroyAPIView): # 查询单条、更新和删除
queryset = Book.objects.all()
serializer_class = BookSerializer
基于ModelViewSet继承,编写5个类
要编写五个接口,对应写两个视图类,配置两条路由,通过观察发现连个视图类的代码几乎一模一样,我们借此引入了ModelViewSet类,继承它只需要编写一个视图类
改变了路由写法—>
path('books/', views.BookAPIView.as_view({'get':'list','post':'create'})),
path('books/<int:pk>', views.BookAPIView.as_view({'get':'retrieve','put':'update','delete':'destroy'})),
在路由里面指名两个get方法分别对应的类具体方法,当是get请求,访问这个地址,就执行视图类的list方法或retrieve方法
视图类:
from rest_framework.viewsets import ModelViewSet
class BookAPIView(ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
点开ModelViewSet源码可知,ModelViewSet封装了ListAPIView、 CreateAPIView、RetrieveAPIView、UpdateAPIView、DestroyAPIView,直接继承ModelViewSet可以直接使用create、list、retrieve、destroy和update五种方法,但是我们查询单表和多条都是get的方法,所以需要在路由指名两个路由的get方法分别对应内置的方法( path(‘books/’, views.BookAPIView.as_view({‘get’: ‘list’, ‘post’: ‘create’})))
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!