当前位置:网站首页>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_)
效果图
边栏推荐
- How to use pywinauto and pyautogui to link the anime lady and sister please go home
- PostgreSQL 基础--常用命令
- excel vertical to horizontal
- excel vertical to horizontal
- Graph Theory - Strongly Connected Component Condensation + Topological Sort
- 毕业作业
- (Translation) How the contrasting color of the button guides the user's actions
- 添加大量元素时使用 DocumentFragments
- JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method
- xctf attack and defense world web master advanced area web2
猜你喜欢
随机推荐
ping no reply
部门项目源码分享
PHP算法之电话号码的字母组合
excel remove all carriage return from a cell
visual studio code multiple editing
字符串——Trie
10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
Three, mysql storage engine - building database and table operation
excel change cell size
SRv6 L3VPN的工作原理
TCP 可靠吗?为什么?
Error creating bean with name ‘dataSource‘:Unsatisfied dependency expressed through field ‘basicPro
(Translation) How the contrasting color of the button guides the user's actions
Use Jenkins for continuous integration, this knowledge point must be mastered
小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书
基于JAX的激活函数、softmax函数和交叉熵函数
C#大型互联网平台管理框架源码:基于ASP.NET MVC+EF6+Bootstrap开发,支持多数据库
Prufer sequence
小程序毕设作品之微信体育馆预约小程序毕业设计成品(1)开发概要
String - Trie









