当前位置:网站首页>DRF generating serialization class code
DRF generating serialization class code
2022-08-01 23:08:00 【Ali's love letter】
一、简介
drfIt is too cumbersome to generate serialization class code,So here the code is automatically generated and then pasted.
二、使用方法
- 导入models中的类
- Copy the run results to
serializer.py
文件中.
The places that need to be modified have been commented in the code
import django, os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "netshop.settings") # 将'netshop'Change it to your own project name
django.setup()
''' Import your own model classes '''
from cartapp.models import CartItem
from goodsapp.models import Category, Goods, GoodsDetailName, GoodDetail, Size, Color, Inventory
from orderapp.models import Order, OrderItem
from userapp.models import Aarea, UserInfo, Address
def generate_serializers(class_):
all_fields = getattr(class_, 'fields', [field.name for field in class_._meta.fields])
class_str = str(class_).split(".")[-1].replace("'>", "")
msg = f'''class {
class_str}Serializer(serializers.ModelSerializer): class Meta: model = {
class_str} fields = {
str(all_fields)}'''
return msg
def serializer_run(class_:list)-> None:
for code_text in map(generate_serializers, class_):
print(code_text, "\n")
''' Modify it to your own model class '''
clss_ = [Category, Goods, GoodsDetailName, GoodDetail, Size, Color, Inventory,Order, OrderItem,Aarea, UserInfo, Address]
serializer_run(clss_)
效果图
边栏推荐
- Interpretation of the paper (GSAT) "Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism"
- 还在纠结报表工具的选型么?来看看这个
- 华为无线设备配置双链路冷备份(AP指定配置方式)
- Three, mysql storage engine - building database and table operation
- SRv6 L3VPN的工作原理
- ROS2初级知识(8):Launching启动多节点
- xctf attack and defense world web master advanced area webshell
- 访问控制台中的选定节点
- Nacos配置中心之加载配置
- 10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
猜你喜欢
APP专项测试:流量测试
【数据分析03】
10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
Small application project works WeChat stadium booking applet graduation design of the finished product (1) the development profile
Deep Learning Course2 Week 2 Optimization Algorithms Exercises
C语言——分支语句和循环语句
文件查询匹配神器 【glob.js】 实用教程
解决yolov5训练时出现:“AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train ”
请问什么是 CICD
Mini Program Graduation Works WeChat Food Recipe Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template
随机推荐
C#大型互联网平台管理框架源码:基于ASP.NET MVC+EF6+Bootstrap开发,支持多数据库
如何给 UE4 场景添加游戏角色
Postman batch test interface detailed tutorial
部门项目源码分享
计算由两点定义的线的角度
excel remove all carriage return from a cell
APP专项测试:流量测试
1391D. 505 状压dp
对于在新标签页中打开的链接,始终使用“noopener”或“noreferrer”
还在纠结报表工具的选型么?来看看这个
13、学习MySQL 分组
bat 之 特殊字符&转义
数据分析04
ping no reply
用virtualenv和Virtualenvwrapper虚拟环境管理工具创建虚拟环境
Is TCP reliable?Why?
excel change cell size
复现gallerycms字符长度限制短域名绕过
SQL29 Calculate the average next day retention rate of users
Jmeter是什么