当前位置:网站首页>Flash operates on multiple databases
Flash operates on multiple databases
2022-04-23 17:59:00 【Feng Ye 520】
config.py:
SQLALCHEMY_BINDS = {
"db1": "mysql+pymysql://db_user1:[email protected]_host1:db_port1/db_name1?charset=utf8",
"db2": "mysql+pymysql://db_user2:[email protected]_host2:db_port2/db_name2?charset=utf8",
}
models.py:
def dynamic_model(bind_key, model_n):
properties = {
"__tablename__" = model_n.__tablename__,
"__bind_key__" = bind_key
}
for col in dir(model_n):
if col.startswith('_'):
continue
properties[col] = getattr(model_n, col)
model = type(model_n.__tablename__, (db.Model,), properties)
return model
Custom Serialization :
class Serialize:
def __init__(self, model):
self.query = model.query
self.model = model
self.columns = []
self.page_size = request.args.get('page_size', type=int, default=10)
self.current_page = request.args.get('page', default=1, type=int)
self.start = (self.current_page - 1) * self.page_size
self.end = self.start + self.page_size
self.field_func_map = {}
def field(self, *columns):
self.columns = columns
self.query = db.session.query(*[getattr(self.model, column) for column in columns])
return self
def search(self, **kw_map):
for column in kw_map:
if kw_map[column]:
self.query = self.query.filter(
getattr(self.model, column).like("%{}%".format('/' + kw_map[column]), escape='/'))
return self
def exclude(self, **kw_map):
for column in kw_map:
self.query = self.query.filter(
getattr(self.model, column) != kw_map[column])
return self
def custom_handle(self, **kargs):
self.field_func_map = kargs
return self
def to_json(self, **kw_map):
res = []
for column in kw_map:
self.query = self.query.filter(
getattr(self.model, column) == kw_map[column])
if self.columns:
for q in self.query.slice(self.start, self.end):
obj_dict = {}
for i, c in enumerate(self.columns):
if c in self.field_func_map:
obj_dict[c] = self.field_func_map[c.name](q[i])
else:
obj_dict[c] = q[i]
res.append(obj_dict)
else:
for q in self.query.slice(self.start, self.end):
obj_dict = {}
for c in class_mapper(q.__class__).columns:
if c.name in self.field_func_map:
obj_dict[c.name] = self.field_func_map[c.name](getattr(q, c.name))
else:
obj_dict[c.name] = getattr(q, c.name)
res.append(obj_dict)
return res
def total(self):
return self.query.count()
def data(self, **kw_map):
return {
'data': self.to_json(**kw_map),
'total': self.total(),
'currentPage': self.current_page,
'pageSize': self.page_size
}
版权声明
本文为[Feng Ye 520]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230546133951.html
边栏推荐
- Amount input box, used for recharge and withdrawal
- YOLOv4剪枝【附代码】
- 2022年茶艺师(初级)考试模拟100题及模拟考试
- Oil monkey website address
- 2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
- String function in MySQL
- Build openstack platform
- Go对文件操作
- Halo open source project learning (II): entity classes and data tables
- C language implements memcpy, memset, strcpy, strncpy, StrCmp, strncmp and strlen
猜你喜欢

JS parsing and execution process

On the method of outputting the complete name of typeID from GCC

Using files to save data (C language)

Data stream encryption and decryption of C

Fashion classification case based on keras

Process management command
![C1 notes [task training chapter I]](/img/2b/94a700da6858a96faf408d167e75bb.png)
C1 notes [task training chapter I]
![C1 notes [task training part 2]](/img/10/48f7490a6c097f2b178ae948cb2c91.png)
C1 notes [task training part 2]

SystemVerilog (VI) - variable

2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination
随机推荐
【Appium】通过设计关键字驱动文件来编写脚本
Oil monkey website address
Implementation of k8s redis one master multi slave dynamic capacity expansion
Secure credit
C language loop structure program
C1 notes [task training chapter I]
C language implements memcpy, memset, strcpy, strncpy, StrCmp, strncmp and strlen
587. Install fence / Sword finger offer II 014 Anagrams in strings
Realsense selection comparison d455 d435i d415 t265 3D hardware comparison
2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
C1小笔记【任务训练篇一】
C# 网络相关操作
Remember using Ali Font Icon Library for the first time
Leak detection and vacancy filling (6)
纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资
C [file operation] read TXT text by line
Vite configure proxy proxy to solve cross domain
Classes and objects
Random number generation of C #
YOLOv4剪枝【附代码】