当前位置:网站首页>drf生成序列化类代码
drf生成序列化类代码
2022-08-01 23:03:00 【阿狸的情书】
一、简介
drf生成序列化类代码时候实在太麻烦,所以这里用代码自动生成后粘贴即可。
二、使用方法
- 导入models中的类
- 将运行结果复制到
serializer.py文件中。
需要修改的地方已经在代码中注释
import django, os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "netshop.settings") # 将'netshop'修改成自己的项目名
django.setup()
''' 导入自己的模型类 '''
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")
''' 修改为自己的模型类 '''
clss_ = [Category, Goods, GoodsDetailName, GoodDetail, Size, Color, Inventory,Order, OrderItem,Aarea, UserInfo, Address]
serializer_run(clss_)
效果图
边栏推荐
- SAP Spartacus NgExpressEngineDecorator 的工作原理
- 分享10套开源免费的高品质源码,免费源码下载平台
- excel clear format
- excel cell contian two words, seperated by a slash
- Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D Solution
- bat 之 特殊字符&转义
- 深度学习Course2第一周Practical aspects of Deep Learning习题整理
- 1391D. 505 状压dp
- qt-faststart 安装使用
- Deep learning Course2 first week Practical aspects of Deep Learning exercises
猜你喜欢

关于ETL的两种架构(ETL架构和ELT架构)

APP专项测试:流量测试

JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method

03. GO language variable definition, function

The must-have "fishing artifact" for programmers is here!

解决端口占用

域名重定向工具 —— SwitchHosts 实用教程

10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享

(翻译)按钮的对比色引导用户操作的方式

小程序毕设作品之微信体育馆预约小程序毕业设计成品(1)开发概要
随机推荐
图论——强连通分量缩点+拓扑排序
华为无线设备配置全局双链路冷备份(AC全局配置方式)
小程序毕设作品之微信体育馆预约小程序毕业设计成品(2)小程序功能
xctf attack and defense world web master advanced area webshell
从0到1:图文投票小程序设计与研发笔记
隔离和降级
联邦学习入门
三、mysql 存储引擎-建库建表操作
将vim与系统剪贴板的交互使用
JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法
excel clear format
SQL Server (design database--stored procedure--trigger)
excel vertical to horizontal
【数据分析03】
vscode hide menu bar
xctf attack and defense world web master advanced area web2
PDF转Word有那么难吗?做一个文件转换器,都解决了
(Translation) How the contrasting color of the button guides the user's actions
No more rolls!After joining ByteDance for a week, he ran decisively.
Interpretation of the paper (GSAT) "Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism"