python-3.x - Access dynamically created tables SQLAlchemy
问题描述
Following the Tutorial on this site: http://sparrigan.github.io/sql/sqla/2016/01/03/dynamic-tables.html. I was trying to create an table using type.The function below is located in a file called models.py in my app directory.
def dynamic_class(table_name, columns):
dyn_table=type(table_name, (Base,), columns)
return dyn_table
From a python shell, I was trying to create and access a dynamic class with the following steps:
from app.models import dynamic_class
attr_dict = {'__tablename__': 'testclass6','col1': db.Column(db.Integer, primary_key=True),'col2': db.Column(db.Integer)}
dynamic_class('Testclass6', attr_dict)
<class 'app.models.Testclass6'>
But, if I'm now trying to access this created class
Traceback (most recent call last): File "", line 1, in NameError: name 'Testclass6' is not defined
So my question is, how to access it?
Thank you, please be polite I'm new to programming :-)
解决方案
推荐阅读
- ruby-on-rails - 如何在完全隔离的 chrome 实例中运行 rails 应用程序?
- azure - 如何在逻辑应用程序中计算以小时为单位的日期差异
- typescript - 尝试使用 tsyringe 和 typeorm 进行依赖注入时遇到问题
- class - 为什么嵌套类使包含类可以访问子类的受保护数据?
- python - 使用 Pandas 为靠得太近的数字添加缓冲区
- vb.net - 使用时间跨度值更改日期的时间部分
- excel - INDEX MATCH 基于多个标准。一个是 FIND,另一个是 Max date
- python - 基于groovy代码生成HmacSHA256到Python中
- c++ - 无法从 decltype 中获取指向成员函数的指针
- android - 有线问题,RecyclerView 在某些特定的三星 Galaxy 设备上不显示项目