(生鲜项目)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 . 刷新网页看看结果

第二步: 使用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, 以及分页的设置的更多相关文章
- 5- vue django restful framework 打造生鲜超市 -完成商品列表页(上)
使用Python3.6与Django2.0.2(Django-rest-framework)以及前端vue开发的前后端分离的商城网站 项目支持支付宝支付(暂不支持微信支付),支持手机短信验证码注册, ...
- (生鲜项目)07. api view实现商品列表页
第一步: 环境配置 1. DRF官网: https://www.django-rest-framework.org/ 仔细查看自己当前的python版本以及django版本是否支持DRF, 然后就看看 ...
- (生鲜项目)06. django的view实现商品列表页
使用原始的django的View来返回json格式的商品列表 目的是回顾一些django的基础知识, 好与后面的RESTful做对比 goods.views_base.py from django.v ...
- 6- vue django restful framework 打造生鲜超市 -完成商品列表页(下)
Vue+Django REST framework实战 搭建一个前后端分离的生鲜超市网站 Django rtf 完成 商品列表页下 drf中的request和response drf对于django的 ...
- react 从商品详情页返回到商品列表页,列表自动滚动上次浏览的位置
现状:目前从商品详情页返回到商品列表页,还需要再去请求服务数据,还需要用户再去等待获取数据的过程,这样用户体验非常不好, 遇到的问题: 1:如何将数据缓存, 2:如何获取和保存列表滑动的高度, 3:判 ...
- DJANGO-天天生鲜项目从0到1-008-列表页
本项目基于B站UP主‘神奇的老黄’的教学视频‘天天生鲜Django项目’,视频讲的非常好,推荐新手观看学习 https://www.bilibili.com/video/BV1vt41147K8?p= ...
- 【vue】饿了么项目-goods商品列表页开发
1.flex 属性是 flex-grow.flex-shrink 和 flex-basis 属性的简写属性. flex-grow 一个数字,规定项目将相对于其他灵活的项目进行扩展的量. flex-sh ...
- 拼团商品列表页 分析 js代码行位置对执行的影响和window.onload的原理 setTimeout传参
w TypeError : Cannot set property 'innerHTML' of nullTypeError : Cannot set property 'value' of null ...
- Vue nodejs商城项目-商品列表价格过滤和加入购物车功能
一.价格过滤功能 GoodsList.vue >>点击价格区间时发送请求 methods:{ getGoodsList(flag){ var param = { ...
随机推荐
- Java8-ConcurrentUtils
import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; public class Conc ...
- C# 委托实现冒泡排序
委托实现员工根据工资升序排列 首先定义员工类 class Employee { public string Name { get; private set; } public decimal Sala ...
- CentOS:去掉警报声音
vi /etc/inputrc 然后将set bell-style none前面的#删掉 none 改为off :wq 保存退出 vim /etc/bashrc 在开始的地方加上一句 setterm ...
- 关于npm audit fix
https://blog.csdn.net/weixin_40817115/article/details/81007774 npm audit : npm@5.10.0 & npm@6,允许 ...
- Base64().encodeBase64Chunked导致换行符的问题
String linkStr=new String(new Base64().encodeBase64Chunked(new String("conferid="+cid+&quo ...
- Vs中包含目录、库目录、附加包含目录、附加库目录、附加依赖项之详解
VS项目中的包含目录.库目录.附加包含目录.附加库目录.附加依赖项均在"项目->属性->配置属性"下进行配置,具体说明如下: VC++目录: 包含目录:寻找#inclu ...
- ZR#954 分组
ZR#954 分组 解法: 设 $ f[i][a][b] $ 表示考虑了排序后的前 $ i $ 个人,目前已经有 $ a $ 个组配好了,还有 $ b $ 个组只有组员没有组长的最小代价.转移时,考虑 ...
- jvm 线程状态
NEW: Just starting up, i.e., in process of being initialized.NEW_TRANS: Corresponding transition sta ...
- 解决python中文乱码的方法
首先需要说明的是,windows下的文件路径,cmd窗口等默认编码都是gbk 但在windows下编写python程序的时候,我们一般采用的编码是utf-8 二者不一致是导致乱码的根本原因! 在pyc ...
- Redis字符串(String)
1.set SET key value [EX seconds] [PX milliseconds] [NX|XX] 将字符串值 value 关联到 key 可选参数: EX second :设置键的 ...