flask/django 動態(tài)查詢表結(jié)構(gòu)相同表名不同數(shù)據(jù)的Model實現(xiàn)方法
1.問題
為了控制數(shù)據(jù)的增長,經(jīng)常需要分表,數(shù)據(jù)庫中存在多張結(jié)構(gòu)相同,表名相關的表,如:
table_201706
table_201707
table_201708
怎么通過SQLAlchemy 或者django查詢相關的數(shù)據(jù)表,而不用每次都創(chuàng)建Model呢
2.解決方法
分別在flask和django下實現(xiàn),代碼如下
2.1 flask+sqlalchemy
# -*-coding:utf-8
class NewDynamicModel(object):
"""
動態(tài)產(chǎn)生模型和表的對應關系模型
:param base_cls: 基類模型,虛類,如TemplateModel
:param tb_name: 數(shù)據(jù)庫中對應的表名, 如tb_test_2017
:return: Model class
eg:
'''
class TemplateModel(db.Model):
__abstract__ = True
id = db.Column(db.Integer(), autoincrement=True, primary_key=True)
name = db.Column(db.VARCHAR(32), nullable=False)
Test_2017 = NewDynamicModel(TemplateModel, 'tb_test_2017')
print Test_2017.query.all()
'''
"""
_instance = dict()
def __new__(cls, base_cls, tb_name):
new_cls_name = "%s_To_%s" % (
base_cls.__name__, '_'.join(map(lambda x:x.capitalize(),tb_name.split('_'))))
if new_cls_name not in cls._instance:
model_cls = type(new_cls_name, (base_cls,),
{'__tablename__': tb_name})
cls._instance[new_cls_name] = model_cls
return cls._instance[new_cls_name]
Bug:由于新的數(shù)據(jù)模型是通過type動態(tài)創(chuàng)建的,實際module 下的py文件并不存在該類的代碼,因而在通過pickle等方式序列化的時候,會報找不到類的錯誤。
Fixbug: 通過inspect庫,拷貝基類的代碼作為副本,并替換tablename 屬性,寫入臨時類定義文件,并引入module。
新的方式實現(xiàn)如下:
class NewDynamicModel(object):
"""
動態(tài)產(chǎn)生模型和表的對應關系模型
:param base_cls: 基類模型,虛類,如TemplateModel
:param tb_name: 數(shù)據(jù)庫中對應的表名, 如tb_test_2017
:return: Model class
eg:
'''
class TemplateModel(db.Model):
__abstract__ = True
id = db.Column(db.Integer(), autoincrement=True, primary_key=True)
name = db.Column(db.VARCHAR(32), nullable=False)
Test_2017 = NewDynamicModel(TemplateModel, 'tb_test_2017')
print Test_2017.query.all()
'''
"""
@staticmethod
def get_import_codes(Model):
"""
獲取基類的import依賴
:param Model:
:return:
"""
module = inspect.getmodule(Model)
all_codelines = inspect.getsourcelines(module)
import_codes = []
import_codes.append('# -*-coding:utf-8\n')
for i in all_codelines[0]:
match = re.search(r'[from]*[\w|\s]*import [\w|\s]*', i)
if match:
import_codes.append(i)
import_codes.extend(['\n', '\n'])
return import_codes
@staticmethod
def get_codes(Model, new_model_name, tb_name):
"""
獲取基類的實現(xiàn)代碼
:param Model:
:param new_model_name:
:param tb_name:
:return:
"""
codes = inspect.getsourcelines(Model)[0]
result = []
has_alias_tb_name = False
result.append(codes[0].replace(Model.__name__, new_model_name))
for line in codes[1:]:
match = re.search(r'\s+__tablename__\s+=\s+\'(?P<name>\w+)\'', line)
abstract = re.search(r'(?P<indent>\s+)__abstract__\s+=\s+', line)
if abstract:
del line
continue
if match:
name = match.groupdict()['name']
line = line.replace(name, tb_name)
has_alias_tb_name = True
result.append(line)
if not has_alias_tb_name:
result.append("%s__tablename__ = '%s'\n" % (' ', tb_name))
return result
@staticmethod
def create_new_module(module_name, codes):
"""
創(chuàng)建新表映射類的module文件
:param module_name:
:param codes:
:return:
"""
f_path = os.path.join(CURRENT_PATH, '_tmp/%s.py' % module_name)
fp = open(f_path, 'w')
for i in codes:
fp.write(i)
fp.close()
return import_module('sync_session.models._tmp.%s' % module_name)
_instance = dict()
def __new__(cls, base_cls, tb_name):
new_cls_name = "%s_To_%s" % (
base_cls.__name__, ''.join(map(lambda x: x.capitalize(), tb_name.split('_'))))
if tb_name not in engine.table_names():
raise TableNotExist
if new_cls_name not in cls._instance:
import_codes = cls.get_import_codes(base_cls)
class_codes = cls.get_codes(base_cls, new_cls_name, tb_name)
import_codes.extend(class_codes)
new_module_name = new_cls_name.lower()
new_module = cls.create_new_module(new_module_name, import_codes)
model_cls = getattr(new_module, new_cls_name)
cls._instance[new_cls_name] = model_cls
return cls._instance[new_cls_name]
2.2 django
# -*-coding:utf-8
from django.db import models
class NewDynamicModel(object):
"""
動態(tài)產(chǎn)生模型和表的對應關系模型
:param base_cls: 基類模型,虛類,如TemplateModel
:param tb_name: 數(shù)據(jù)庫中對應的表名, 如tb_test_2017
:return: Model class
eg:
'''
class TemplateModel(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
class Meta:
abstract = True
Test_2017 = NewDynamicModel(TemplateModel, 'tb_test_2017')
print Test_2017.objects.all()
'''
"""
_instance = dict()
def __new__(cls, base_cls, tb_name):
new_cls_name = "%s_To_%s" % (
base_cls.__name__, '_'.join(map(lambda x:x.capitalize(),tb_name.split('_'))))
if new_cls_name not in cls._instance:
new_meta_cls = base_cls.Meta
new_meta_cls.db_table = tb_name
model_cls = type(new_cls_name, (base_cls,),
{'__tablename__': tb_name, 'Meta': new_meta_cls, '__module__': cls.__module__})
cls._instance[new_cls_name] = model_cls
return cls._instance[new_cls_name]
以上這篇flask/django 動態(tài)查詢表結(jié)構(gòu)相同表名不同數(shù)據(jù)的Model實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python計算三角函數(shù)之a(chǎn)sin()方法的使用
這篇文章主要介紹了Python計算三角函數(shù)之a(chǎn)sin()方法的使用,是Python入門的基礎知識,需要的朋友可以參考下2015-05-05
windows下python 3.9 Numpy scipy和matlabplot的安裝教程詳解
這篇文章主要介紹了windows下python 3.9 Numpy scipy和matlabplot的安裝教程詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
淺談PyTorch的可重復性問題(如何使實驗結(jié)果可復現(xiàn))
今天小編就為大家分享一篇淺談PyTorch的可重復性問題(如何使實驗結(jié)果可復現(xiàn)),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python實現(xiàn)定時監(jiān)測網(wǎng)站運行狀態(tài)的示例代碼
這篇文章主要介紹了Python實現(xiàn)定時監(jiān)測網(wǎng)站狀態(tài)的示例代碼,幫助大家更好的管理自己的網(wǎng)站,感興趣的朋友可以了解下2020-09-09
python??UPX?is?not?available問題解決方法
這篇文章主要介紹了python?UPX?is?not?available問題解決,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
Python數(shù)據(jù)分析之Excel和Text文件的讀寫操作方法
Python操作Excel分為兩個主要形式,讀寫和交互式操作,可以用不同的第三方工具,下面這篇文章主要給大家介紹了關于Python數(shù)據(jù)分析之Excel和Text文件的讀寫操作方法,需要的朋友可以參考下2024-08-08

