本文主要是介绍Python Django 之序列化器Serializer高阶使用多表[查询,添加,修改] 带分页,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用版本开发
> Win10
> python 3.7.0
> Django 1.11
前言
今天主要讲一下django serializers 序列化的一些高级一点的使用方法,Django 作为web 开发可以说是相当的方便, 集成工具, 实现快速开发, 序列化呢更是一种高级的查询方法, 苦恼的是以前一直不知道怎么去使用多表添加 修改等等, 所以经过几天的苦修啊, 也算是勉强搞懂了其中的一点真意, 接下来就进入正题吧, 我把所理解的分享给大家!, 所谓实践才是唯一的真理.
1. 创建一个开发测试项目(首先需要一个文件夹名字自定义, 打开文件夹shift+右键 打开Bash 命令行, 我使用的是Git):
# 创建项目CESHI, 不用在意我的命名
django-admin startproject CESHI
2. cd CESHI/CESHI/ 目录下 创建 apps 子应用文件件, 再 cd /apps 下:
# 创建子应用, 我是以商品为例
django-admin startapp goods
3. 配置settings 注册子应用,rest_framework 连接mysql 等等, 直接亮配置吧(别忘记路由分发这些基础的):
"""
Django settings for CESHI project.Generated by 'django-admin startproject' using Django 1.11.For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""# SECURITY WARNING: keep the secret key used in production secret!import os# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
SECRET_KEY = 'l#i^jdx3n&uvxhn=4qg--c95u-c!76$+77)a1v(7*^214@=juq'import sys
# 为了保证应用的注册和导包正常,需要追加导包路径执行'apps'
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = TrueALLOWED_HOSTS = ["*"]# Application definition
INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','goods.apps.GoodsConfig', #注册子应用'rest_framework', # 注册 rest_framework
]MIDDLEWARE = ['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware',
]ROOT_URLCONF = 'CESHI.apps.goods.urls'TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [os.path.join(BASE_DIR,'templates')],'APP_DIRS': True,'OPTIONS': {'context_processors': ['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',],},},
]WSGI_APPLICATION = 'CESHI.wsgi.application'# DatabaseDATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','HOST': '主机IP', # 数据库主机'PORT': 3306, # 数据库端口'USER': 'root', # 数据库用户名'PASSWORD': '****', # 数据库用户密码'NAME': 'ceshi', # 数据库名字# 修复多对多偏离'OPTIONS': {"init_command": "SET foreign_key_checks = 0;",'charset': 'utf8',},# 连接池时间'CONN_MAX_AGE': 300,}
}AUTH_PASSWORD_VALIDATORS = [{'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',},{'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',},{'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',},{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',},
]LANGUAGE_CODE = 'zh-Hans'TIME_ZONE = 'Asia/Shanghai'USE_TZ = FalseSTATIC_URL = os.path.join(BASE_DIR,'static/')
4. 项目搭建完成, 接下来在子应用goods下的model.py 创建ORM模型类(不过多创建字段, 能理解就行):
# 因为版本之间的不兼容性, 所以 导入 unicode_literals 可以兼容下一个版本
from __future__ import unicode_literals
from django.db import models# 品牌
class Brand(models.Model):name = models.CharField(max_length=20, verbose_name='名称')'''...其他字段省略....'''class Meta:db_table = 'tb_brand'def __str__(self):return self.name# 分类
class Cate(models.Model):brand = models.ForeignKey(Brand, on_delete=models.CASCADE, related_name='brand_cate', null=True, blank=True, verbose_name='外键关联')cate_name = models.CharField(max_length=100, verbose_name='分类名称')'''...其他字段省略....'''class Meta:db_table = 'tb_cate'# 商品
class Goods(models.Model):cate = models.ForeignKey ( Cate, on_delete=models.CASCADE, related_name='cate_goods', null=True, blank=True,verbose_name='外键关联' )goods_name = models.CharField(max_length=50, verbose_name='商品名称')goods_size = models.CharField(max_length=50, verbose_name='商品规格')goods_count = models.CharField(max_length=100, verbose_name='商品数量')'''...其他字段省略....'''class Meta:db_table = 'tb_goods'def __str__(self):return self.goods_name
5. 模型创建完毕, 迁移模型到mysal 生成表结构:
# cd 项目的 manage.py 目录下执行python manage.py makemigrationspython manage.py migrate
6. 表结构有了 开始在goods下的view.py 创建API 接口 提供调用:
from __future__ import unicode_literals # 兼容版本
from .serializers import * # 在导包的过程中尽量不要使用 * ,我这里是因为都用到了
from rest_framework.views import APIView # 前后端分离API[ 它的底层是dispatch()] 其实最好用的还是viewSet
from rest_framework.response import Response #返回前端响应
from CESHI.utils.paginators import Paginators #自定义分页类class GoodsListView(APIView):''':param current_page 当前页:param data_list 查询数据集:param page_count 总页数:param data 获取前端传递数据集'''def get(self, request):'''查询数据集'''# 获取当前页值, 没有默认为第一页current_page = request.GET.get('page',1)print(current_page)# 自定义分页类, 第一值: current_page, 第二值: orm模型实例 返回serializer序列化数据并且分页总页数data_list,page_count = Paginators(current_page=current_page,instance_objects=Brand).page_dispose()print(data_list)return Response({"mes":data_list,"page_count":page_count})def post( self, request):'''添加数据集'''data = request.dataprint(data)brand = BrandSerializer(data=data)if brand.is_valid():brand.save()return Response ( {'code': 200,"message":'SUCCESS'} )else:print(brand.errors)return Response ( {'code': 400, "message":'ERROR'} )def put( self, request):'''修改数据集'''data = request.databrand_instance = Brand.objects.get(id=data['id'])brand = BrandSerializer(brand_instance,data)if brand.is_valid():brand.save()return Response ( {'code': 200,"message":'SUCCESS'} )else:print(brand.errors)return Response ( {'code': 400, "message":'ERROR'} )class CateListAPI(APIView):def get( self, request):data = Cate.objects.filter()cate_goods_list =CatesSerializers(data, many=True)return Response(cate_goods_list.data)
7. 需要自定义序列化, 所有在 goods 下 创建 serializers.py 写入一下deMo(我这里是使用的三张表的查询):
from goods.models import *
from rest_framework import serializers'''
这种方法的序列化多表查询嵌套添加修改, 必须有外键关联.....
'''class GoodsSerializer(serializers.ModelSerializer):class Meta:model = Goodsfields = ('id', 'goods_name', 'goods_count', 'goods_size')class CateSerializer(serializers.ModelSerializer):cate_goods = GoodsSerializer ( many=True )class Meta:model = Catefields = ('id', 'cate_name', 'cate_goods')class BrandSerializer(serializers.ModelSerializer):brand_cate = CateSerializer ( many=True )class Meta:model = Brandfields = ('id', 'name', 'brand_cate')def create(self, validated_data):# pop 弹出data 中的 数据并保存brand_cates = validated_data.pop('brand_cate')# 并进行第一张表的添加brand = Brand.objects.create(**validated_data)# 遍历 弹出的第二张表的数据并添加第二章表, 且 主键信息为单独添加for cate_data in brand_cates:cate_goods = cate_data.pop ( 'cate_goods' )cate = Cate.objects.create(brand=brand, **cate_data)# 第三张表数据添加for goods in cate_goods:Goods.objects.create (cate=cate, **goods)return branddef update(self, instance, validated_data):# 获取当前json 数据中 外键表的数据brand_cate = validated_data.pop('brand_cate')# 通过 instance 实例 去获取它外键表的实例brand_cate_data = (instance.brand_cate).all()brand_cate_data = list(brand_cate_data)# 修改 当前实例表的数据instance.name = validated_data.get('name', instance.name)instance.save()# 遍历 pop弹出的 外键表要修改的数据for cate_data in brand_cate:# 继续弹出第三张表外键的数据goods_data = cate_data.pop ( 'cate_goods' )# 获取第一张外键的 实例cate_instance = brand_cate_data.pop(0)# 获取第三张表的实例cate_goods = list((cate_instance.cate_goods).all())# 更新 第一张外键表数据cate_instance.name = cate_data.get('cate_name', cate_instance.cate_name)cate_instance.save()# 遍历继续更新第三张表的数据for goods in goods_data:goods_instance = cate_goods.pop(0)goods_instance.goods_name = goods.get("goods_name",goods_instance.goods_name)goods_instance.goods_count = goods.get("goods_count",goods_instance.goods_count)goods_instance.goods_size = goods.get("goods_size",goods_instance.goods_size)goods_instance.save()return instance# 以下呢是不同的方法,去测试了一下都是可以的但是 分页还没做的哦.
class GoodsSerializersModel(serializers.ModelSerializer):# 通过 source 去指向源地址, 也就是cate外键 数据中的 name 值cate_name = serializers.CharField ( source="cate.cate_name" )class Meta:model = Goodsfields = '__all__'class CatesSerializers(serializers.ModelSerializer):'''通过这种方法也可以进行数据嵌套返回'''# SerializerMethodField () 父类序列化容器, 通过它可以去查询并并返回相关的数据goodsList = serializers.SerializerMethodField ()# get__ 必须跟 前面定义的变量, row 值表示父类容器的当前这一条数def get_goodsList ( self, row ):try:# 通过当前row 的 id 去查询外键数据goodsQuery = Goods.objects.filter ( cate=row.id ).all ()# 查询到的QuerySet 通过 自定义的序列化容器 返回响应的数据goodsList = GoodsSerializersModel ( goodsQuery, many=True )return goodsList.dataexcept:return [ ]class Meta:model = Catefields = '__all__'
8. 为了实现嵌套层级的多表还带分页, 所以还得自定义一个分页的py文件(所以在utils 下创建 paginators.py)写入如下demo(实现分页效果):
from goods.serializers import BrandSerializerclass Paginators(object):''':param self.current_page 当前页:param self.modelObjects 传入模型实例:param self.page_count 总页数:param self.start_data 起始位置:param self.end_data 结束位置:return data.data 序列化返回的JSON数据'''def __init__(self, current_page, instance_objects):'''静态初始化'''self.current_page = current_pageself.modelObjects =instance_objectsself.page_count = 0self.start_data = 0self.end_data = 0def page_dispose (self):current_page = int ( self.current_page )show_num = 10 # 显示条数# 切片的大致规律如下"""1-10条 [0:10] 1 [(1-1)*10:1*10]11-20条 [10:20] 2 [(2-1)*10:2*10]21-30条[20:30] 3 [(3-1)*10:3*10]m-n条 [m-1:n] h [(m-1)*10:n*10]"""self.start_data = (current_page - 1) * show_num # 从第几条数据显示self.end_data = current_page * show_num # 显示到第几条结束count = self.modelObjects.objects.count () # 数据的总数量# divmod() 得余数 cunt 除 show_num 整数位m 余数为nm, n = divmod ( count, show_num )# 有余数多一页,没有就取 m 为总页数if n == 0:num_pages = melse:num_pages = m + 1# 切片操作获取分页数据datalist = self.modelObjects.objects.all ()[ self.start_data:self.end_data ]# 数据序列化data = BrandSerializer(datalist, many=True)return data.data,num_pages
9. 最后附上测试数据, 我使用的是ApiPost 工具, 非常方便,可以生成接口文档哦
# Post
{"name": "贵人鸟","brand_cate": [{"cate_name": "鞋子","cate_goods":[{"goods_name":'透气鞋子-GRN-2020-31',"goods_count":'200',"goods_size":'39,40,41',}]},{"cate_name": "服装","cate_goods":[{"goods_name":'运动服饰-GRN-2020-23',"goods_count":'12',"goods_size":'XL,L,M',}]}]
}# PUT
{'id': 1,"name": "贵人鸟","brand_cate": [{'id': 1,'brand': 1,"cate_name": "鞋子-跑步鞋","cate_goods":[{'id': 1,'cate': 1,"goods_name":'透气鞋子-GRN-2020-31-02',"goods_count":'210',"goods_size":'39,40,41',}]},{'id': 2,'brand': 1,"cate_name": "服装-运动版","cate_goods":[{'id': 2,'cate': 2,"goods_name":'运动服饰-GRN-2020-23',"goods_count":'121',"goods_size":'XL,L,M',}]}]
}
结束语
这样的结合呢, 其实就是多表数据分页嵌套序列化, 但是呢,我只需要先查主表,先进行分页之后呢, 再进行序列化的多表操作, 好了今天就到这里了, 有问题评论下方留言, 我空了会把项目源码传到 github 上,有需要的可以拉取下来看一下, 不知道有没有更好的操作方法, 可以交流哦!
github 源码地址: django-serializers
这篇关于Python Django 之序列化器Serializer高阶使用多表[查询,添加,修改] 带分页的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!