当前位置:网站首页>ORM implements the mapping relationship between classes and tables, class attributes and fields
ORM implements the mapping relationship between classes and tables, class attributes and fields
2022-06-12 21:26:00 【Love letter from Ali】
# class -> surface
# Class properties -> Field
class model:
def __init__(self, max_length=100):
self.max_length = max_length
self.table = self.__class__.__name__
self.fields = [i for i in dir(self) if i not in ['save', 'table', 'max_length'] and i[0:2] != '__']
values = [getattr(self, i) for i in self.fields]
def save(self):
table = self.table
fields = self.fields
fields_sql = [f'`%s` varchar({
self.max_length}) not null,\n' % i for i in fields]
my_fields_sql = "".join(fields_sql)
sql = f'''create table if not exists `{
table}`(`id` int unsigned auto_increment,\n''' + my_fields_sql + 'primary key (`id`))engine=InnoDB default charset=utf8; '
print(sql)
class CharField(model):
pass
class Cat(model):
head = CharField(max_length=200)
foot = CharField()
Cat().save()
Output results :
create table if not exists `Cat`(`id` int unsigned auto_increment,
`foot` varchar(100) not null,
`head` varchar(100) not null,
primary key (`id`))engine=InnoDB default charset=utf8;
边栏推荐
- RestTemplate的@LoadBalance注解
- 初步了解認識正則錶達式(Regex)
- Lua pattern matching
- 最简单ALV模板
- Composer version degradation
- Experiment 7-2-6 print Yanghui triangle (20 points)
- Solve one-dimensional array prefix sum
- Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
- China hydraulic press market trend report, technical innovation and market forecast
- torch. clamp_ min_ method
猜你喜欢

Data visualization - biaxial comparison effect

lintcode:127 · 拓扑排序

Composer version degradation
![Li Mu [practical machine learning] 1.4 data annotation](/img/e4/2593b1dec04476a9cc3b4af94dc189.jpg)
Li Mu [practical machine learning] 1.4 data annotation

makefile 的ifeq,filter,strip 简单使用

Access control system based on RFID

leetcode:207. Class Schedule Card

#113 Path Sum II

GPU giant NVIDIA suffered a "devastating" network attack, and the number one malware shut down its botnet infrastructure | global network security hotspot on February 28

Data visualization - histogram
随机推荐
Lake shore PT-100 platinum resistance temperature sensor
JS deep and shallow copy
The salted fish has been transmitted for 5W times, and the latest spring recruit face-to-face test questions of bytes have been leaked
【目标检测】|Dive Deeper Into Box for Object Detection 基于FCOS新训练方法
Pointer and array & pointer and const & struct and Const
#981 Time Based Key-Value Store
Image processing 12- image linear blending
Teamwork collaboration application experience sharing | community essay solicitation
#141 Linked List Cycle
冒泡排序
What are the disadvantages of bone conduction earphones? Analysis of advantages and disadvantages of bone conduction earphones
Mxnet record IO details
最简单ALV模板
atoi超强解析
Teambition 协作应用心得分享|社区征文
JdbcTemplate插入并返回主键
Data visualization - biaxial comparison effect
GNS installation and configuration
Redis cluster mget optimization
二分查找