Dynamic Class Creation in SQLAlchemy

本秂侑毒 提交于 2019-11-26 09:23:57

问题


We have a need to create SQLAlchemy classes to access multiple external data sources that will increase in number over time. We use the declarative base for our core ORM models and I know we can manually specify new ORM classes using the autoload=True to auto generate the mapping.

The problem is that we need to be able generate them dynamically taking something like this:

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

stored={}
stored[\'tablename\']=\'my_internal_table_name\'
stored[\'objectname\']=\'MyObject\'

and turning it into something like this dynamically:

class MyObject(Base):
    __tablename__ = \'my_internal_table_name\'
    __table_args__ = {\'autoload\':True}

We don\'t want the classes to persist longer than necessary to open a connection, perform the queries, and then closing the connection. Therefore, ideally, we can put the items in the \"stored\" variable above into a database and pull them as needed. The other challenge is that the object name (e.g. \"MyObject\") may be used on different connections so we cannot define it once and keep it around.

Any suggestions on how this might be accomplished would be greatly appreciated.

Thanks...


回答1:


You can dynamically create MyObject using the 3-argument call to type:

type(name, bases, dict)

    Return a new type object. This is essentially a dynamic form of the 
    class statement... 

For example:

mydict={'__tablename__':stored['tablename'],
        '__table_args__':{'autoload':True},}

MyObj=type(stored['objectname'],(Base,),mydict)
print(MyObj)
# <class '__main__.MyObject'>
print(MyObj.__base__)
# <class '__main__.Base'>
print(MyObj.__tablename__)
# my_internal_table_name
print(MyObj.__table_args__)
# {'autoload': True}


来源:https://stackoverflow.com/questions/2768607/dynamic-class-creation-in-sqlalchemy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!