当前位置:网站首页>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;
边栏推荐
- torch. unique()
- leetcode:207. 课程表
- Product Manager: "click here to jump to any page I want to jump" -- decoupling efficiency improving artifact "unified hop routing"
- 字符串基础知识
- Delphi XE7的蓝牙 Bluetooth
- 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
- ASCII code comparison table
- The Post-00 financial woman with a monthly salary of 2W conquered the boss with this set of report template
- 同花顺能开户吗,在同花顺开户安全么
- Pixel level reconstruction and restoration technology to solve severe image blur
猜你喜欢

Graphics2D类基本使用
![Li Mu [practical machine learning] 1.4 data annotation](/img/e4/2593b1dec04476a9cc3b4af94dc189.jpg)
Li Mu [practical machine learning] 1.4 data annotation

Distributed cloud service developer'allegro Xile technology 'received an angel round financing of US $3million

ASCII code comparison table

Digital intelligence data depth | Bi goes down the altar? It's not that the market has declined, it's that the story has changed

Introduction to the characteristics of balancer decentralized exchange market capitalization robot

torch. Finfo function

#113 Path Sum II

Lombok package is successfully installed, but the runtime prompts that get, set method and constructor solution cannot be found

Data visualization - histogram
随机推荐
Rearrangement exercises
Simple understanding of cap and base theory
atoi超强解析
lintcode:127 · 拓扑排序
To delete a character from a string
How to design a message box through draftjs
Scatter in pytorch_ () function
Lua pattern matching
Product Manager: "click here to jump to any page I want to jump" -- decoupling efficiency improving artifact "unified hop routing"
递归调用知识点-包含例题求解二分查找、青蛙跳台阶、逆序输出、阶乘、斐波那契、汉诺塔。
Allegro Xile technology, a developer of distributed cloud services, received millions of dollars of angel round financing and was independently invested by Yaotu capital
Shell language
Research Report on market supply and demand and strategy of hydraulic operating table industry in China
重排数列练习题
Inrelease: the following signature cannot be verified because there is no public key: no_ PUBKEY EB3E94ADBE1229CF
torch. Finfo function
Simplest ALV template
leetcode:210. Schedule II
remote: Support for password authentication was removed on August 13, 2021
Research Report on market supply and demand and strategy of China's hydraulic hammer industry