(生鲜项目)08. ModelSerializer 实现商品列表页, 使用Mixin来实现返回, 以及更加方便的ListAPIView, 以及分页的设置

第一步: 学会使用ModelSerializer, 并且会使用ModelSerializer相互嵌套功能

1. goods.serializers.py

from rest_framework import serializers

from goods.models import Goods, GoodsCategory


# 让goods的category字段全量显示
class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = "__all__"  # 取所有字段

# 方式一
# class GoodsSerializer(serializers.Serializer):
#     name = serializers.CharField(required=True, max_length=100)
#     click_num = serializers.IntegerField(default=0)
#     goods_front_image=serializers.ImageField()

# 方式二: 用ModelSerializer
class GoodsSerializer(serializers.ModelSerializer):
    category = CategorySerializer()  # 重写category字段,从而实现ModelSerializer的嵌套

    class Meta:
        model = Goods
        # fields = ("name", "click_num", "market_price", "add_time")
        fields = "__all__"  # 取所有字段

2 . 刷新网页看看结果

(生鲜项目)08. ModelSerializer 实现商品列表页, 使用Mixin来实现返回, 以及更加方便的ListAPIView, 以及分页的设置

 

 

第二步: 使用Mixin来实现返回

goods.views.py

from rest_framework import mixins
from rest_framework import generics
from rest_framework.views import APIView
from rest_framework.response import Response

from .models import Goods
from .serializers import GoodsSerializer


# # 方式一, APIview继承了View
# class GoodsListView(APIView):
#     """
#     List all snippets, or create a new snippet.
#     """
#
#     def get(self, request, format=None):
#         goods = Goods.objects.all()[:10]
#         goods_serializer = GoodsSerializer(goods, many=True)  # many:是否是querrySet对象
#         return Response(goods_serializer.data)

# # 方式二: 利用mixins.ListModelMixin, generics.GenericAPIView
# class GoodsListView(mixins.ListModelMixin, generics.GenericAPIView):
#     """
#     商品列表页
#     """
#     queryset = Goods.objects.all()[:10]
#     serializer_class = GoodsSerializer
#
#     def get(self, request, *args, **kwargs):
#         return self.list(request, *args, **kwargs)


# # 方式三,使用更加简便的generics.ListAPIView
# # 原理就是: ListAPIView里面继承了mixins.ListModelMixin, generics.GenericAPIView, 而且还封装了返回
class GoodsListView(generics.ListAPIView):
    """
    商品列表页
    """
    queryset = Goods.objects.all()
    serializer_class = GoodsSerializer

 

第三步: 分页的设置

方法一: 在settings里面设置REST_FRAMEWORK分页参数

settings.py

REST_FRAMEWORK = {
    # 解决 1.11.3版本下使用APIview会报‘CSRFCheck‘ object has no attribute ‘process_request‘的问题
    # "DEFAULT_AUTHENTICATION_CLASSES": [],
    
    DEFAULT_PAGINATION_CLASS: rest_framework.pagination.PageNumberPagination,
    PAGE_SIZE: 10,
}

接着去刷新网页

(生鲜项目)08. ModelSerializer 实现商品列表页, 使用Mixin来实现返回, 以及更加方便的ListAPIView, 以及分页的设置

 

 还有下面这个img字段的url的拼接, 也是genericAPIView帮我们完成的

(生鲜项目)08. ModelSerializer 实现商品列表页, 使用Mixin来实现返回, 以及更加方便的ListAPIView, 以及分页的设置

 

 

方法二: 在goods.view里面重写PageNumberPagination, 从而达到动态分页的目的, 这样前端就想怎么取就怎么取

goods.view.py

from rest_framework import mixins
from rest_framework import generics
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination  # 自定义分页

from .models import Goods
from .serializers import GoodsSerializer

# 自定义分页功能, 并实现动态分页, 继承PageNumberPagination
class GoodsPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = page_size  # 指定每一页显示多少条
    page_query_param = "p"  # 指定要第几页
    max_page_size = 100


class GoodsListView(generics.ListAPIView):
    """
    商品列表页
    """
    queryset = Goods.objects.all()
    serializer_class = GoodsSerializer
    pagination_class = GoodsPagination  # 需要指定分页的类

 

刷新网页试试

(生鲜项目)08. ModelSerializer 实现商品列表页, 使用Mixin来实现返回, 以及更加方便的ListAPIView, 以及分页的设置

 

 

 

 

 

 

 

 

 

 

 

 

------ over  ------

(生鲜项目)08. ModelSerializer 实现商品列表页, 使用Mixin来实现返回, 以及更加方便的ListAPIView, 以及分页的设置

上一篇:Elasticsearch:Cluster备份 Snapshot及Restore API


下一篇:web API接口、restful规范