当前位置:网站首页>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_)
效果图
边栏推荐
- xss相关知识点以及从 XSS Payload 学习浏览器解码
- IDEA入门看这一篇就够了
- chrome复制一张图片的base64数据
- qt-faststart 安装使用
- Background project Express-Mysql-Vue3-TS-Pinia page layout-sidebar menu
- 分享10套开源免费的高品质源码,免费源码下载平台
- excel vertical to horizontal
- 解决yolov5训练时出现:“AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train ”
- 计算两点之间的距离
- 将vim与系统剪贴板的交互使用
猜你喜欢

小程序毕设作品之微信美食菜谱小程序毕业设计成品(8)毕业设计论文模板

SRv6 L3VPN的工作原理

牛客多校4 A.Task Computing 思维
Background project Express-Mysql-Vue3-TS-Pinia page layout-sidebar menu

如何给 UE4 场景添加游戏角色

小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT

TCP 可靠吗?为什么?

下载安装 vscode(含汉化、插件的推荐和安装)

APP special test: traffic test

From 0 to 100: Notes on the Development of Enrollment Registration Mini Programs
随机推荐
cmd指令
excel change cell size
下载安装 vscode(含汉化、插件的推荐和安装)
PHP算法之电话号码的字母组合
数据库表设计规则
访问控制台中的选定节点
excel remove all carriage return from a cell
Postman 批量测试接口详细教程
加载字体时避免隐藏文本
CAKE:一个用于多视图知识图谱补全的可扩展性常识感知框架
移动端人脸风格化技术的应用
ROS2初级知识(8):Launching启动多节点
Chapter 12 End-User Task As Shell Scripts
三、mysql 存储引擎-建库建表操作
drf生成序列化类代码
System availability: 3 9s, 4 9s in SRE's mouth... What is it?
Background project Express-Mysql-Vue3-TS-Pinia page layout-sidebar menu
字符串——Trie
深度学习Course2第一周Practical aspects of Deep Learning习题整理
qt-faststart 安装使用