当前位置:网站首页>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;
边栏推荐
- Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
- How to design a message box through draftjs
- remote: Support for password authentication was removed on August 13, 2021
- #886 Possible Bipartition
- Module 8: Design message queue MySQL table for storing message data
- Research Report on market supply and demand and strategy of hydraulic operating table industry in China
- Structure knowledge points all in
- A high-value MySQL management tool
- Select sort
- Is it safe to open an account in flush? How to open an account online to buy stocks
猜你喜欢

shell语言

Shell script Basics

C language learning notes (II)

Risk control modeling X: Discussion on problems existing in traditional modeling methods and Exploration on improvement methods

一款高颜值的MySQL管理工具

金融信创爆发年!袋鼠云数栈DTinsight全线产品通过信通院信创专项测试

Design and practice of Hudi bucket index in byte skipping

#141 Linked List Cycle

leetcode:207. Class Schedule Card

Mxnet record IO details
随机推荐
ATOI super resolution
How to improve communication efficiency during home office | community essay solicitation
Shell script Basics
Preliminary understanding of regular expressions (regex)
金融信创爆发年!袋鼠云数栈DTinsight全线产品通过信通院信创专项测试
ZGC concurrent identity and multi view address mapping in concurrent transition phase
Lua pattern matching
初步了解認識正則錶達式(Regex)
Structure knowledge points all in
重排数列练习题
初步了解认识正则表达式(Regex)
居家办公期间如何提升沟通效率|社区征文
What are the disadvantages of bone conduction earphones? Analysis of advantages and disadvantages of bone conduction earphones
Data visualization - histogram
shell语言
Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
How to design a message box through draftjs
Ubuntu16.04 completely delete MySQL database
test
torch. clamp_ min_ method