当前位置:网站首页>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
边栏推荐
- I/O多路复用及其相关详解
- JS parsing and execution process
- C byte array (byte []) and string are converted to each other
- In JS, t, = > Analysis of
- ROS package NMEA_ navsat_ Driver reads GPS and Beidou Positioning Information Notes
- Cloud native Virtualization: building edge computing instances based on kubevirt
- [appium] write scripts by designing Keyword Driven files
- 2022江西储能技术展会,中国电池展,动力电池展,燃料电池展
- Eigen learning summary
- 【Appium】通过设计关键字驱动文件来编写脚本
猜你喜欢

An example of linear regression based on tensorflow

cv_ Solution of mismatch between bridge and opencv

Laser slam theory and practice of dark blue College Chapter 3 laser radar distortion removal exercise

C# 的数据流加密与解密

MySQL 中的字符串函数

Go language JSON package usage
![[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)](/img/91/3272d5ad04cd1d8476c739546f4356.png)
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)

Go对文件操作

关于gcc输出typeid完整名的方法

MySQL_01_简单数据检索
随机推荐
Summary of floating point double precision, single precision and half precision knowledge
C1小笔记【任务训练篇二】
2022年广东省安全员A证第三批(主要负责人)特种作业证考试题库及在线模拟考试
How to read literature
102. Sequence traversal of binary tree
Encapsulate a timestamp to date method on string prototype
Nat commun | current progress and open challenges of applied deep learning in Bioscience
Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag
_ FindText error
Error in created hook: "referenceerror:" promise "undefined“
JS parsing and execution process
Implementation of k8s redis one master multi slave dynamic capacity expansion
Theory and practice of laser slam in dark blue College - Chapter 2 (odometer calibration)
MySQL_01_简单数据检索
极致体验,揭晓抖音背后的音视频技术
.105Location
C1 notes [task training part 2]
EasymodbusTCP之clientexample解析
Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战
Eigen learning summary