码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2019-12-23 22:10:22      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:字段   ret   code   多少   使用   price   settings   snippet   自定义分页   

第一步: 学会使用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 . 刷新网页看看结果

技术图片

 

 

第二步: 使用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,
}

接着去刷新网页

技术图片

 

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

技术图片

 

 

方法二: 在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  # 需要指定分页的类

 

刷新网页试试

技术图片

 

 

 

 

 

 

 

 

 

 

 

 

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

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

标签:字段   ret   code   多少   使用   price   settings   snippet   自定义分页   

原文地址:https://www.cnblogs.com/jiangzongyou/p/12088647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!