当前位置:网站首页>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;
边栏推荐
- USB机械键盘改蓝牙键盘
- Binary search
- Ubuntu16.04 completely delete MySQL database
- Recursively call knowledge points - including example solving binary search, frog jumping steps, reverse order output, factorial, Fibonacci, Hanoi tower.
- Data visualization - broken line area chart
- Limit of advanced mathematics
- Can flush open an account? Can you directly open the security of securities companies on the app
- JUC并发工具包使用指南
- What's a good gift for the goddess Festival? Gift recommendation for the goddess Festival on March 8
- 二分查找
猜你喜欢
torch. Finfo function
The year of the outbreak of financial innovation! All dtinsight products of kangaroo cloud data stack have passed the special test of Xinchuang of ICT Institute
【目标检测】|Dive Deeper Into Box for Object Detection 基于FCOS新训练方法
Smart management of green agriculture: a visual platform for agricultural product scheduling
Data visualization - histogram
leetcode:207. 课程表
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
Library cache lock brought by add trandata
Graphics2D类基本使用
Solution of multi machine room dynamic loop status network touch screen monitoring
随机推荐
InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY EB3E94ADBE1229CF
Shell script Basics
#886 Possible Bipartition
Recursively call knowledge points - including example solving binary search, frog jumping steps, reverse order output, factorial, Fibonacci, Hanoi tower.
冒泡排序
USB机械键盘改蓝牙键盘
Fill in the checklist & lt; int> Have default values? [repeat] - fill list & lt; int> with default values? [duplicate]
Structure knowledge points all in
How to improve communication efficiency during home office | community essay solicitation
Ubuntu16.04 completely delete MySQL database
#141 Linked List Cycle
好数对的求解
Insert sort
Binary search
递归调用知识点-包含例题求解二分查找、青蛙跳台阶、逆序输出、阶乘、斐波那契、汉诺塔。
测试基础之:单元测试
nn. PReLU(planes)
[leetcode] 573 complex multiplication (conversion between characters and numbers)
What's a good gift for the goddess Festival? Gift recommendation for the goddess Festival on March 8
Data visualization - biaxial comparison effect