Python 当sqlalchemy ORM类属性与数据库列不同时,如何获取ORM类属性的列表?
假设您想在sqlalchemy中迭代ORM类的ORM属性。 所以,您需要一个ORM属性列表。你是怎么得到这份名单的 如果ORM类没有重命名属性,因此ORM属性与数据库列匹配,则可以使用以下解决方案: (顺便说一句,在源代码文件/lib/sqlalchemy/orm/base.py中还有一个内置(私有)函数_orm_columns(),它似乎提供了此功能) 但是,如果python ORM类的名称与数据库列的名称不同(例如,在这3个ORM属性中): 那么这种方法就行不通了。Python 当sqlalchemy ORM类属性与数据库列不同时,如何获取ORM类属性的列表?,python,sqlalchemy,Python,Sqlalchemy,假设您想在sqlalchemy中迭代ORM类的ORM属性。 所以,您需要一个ORM属性列表。你是怎么得到这份名单的 如果ORM类没有重命名属性,因此ORM属性与数据库列匹配,则可以使用以下解决方案: (顺便说一句,在源代码文件/lib/sqlalchemy/orm/base.py中还有一个内置(私有)函数_orm_columns(),它似乎提供了此功能) 但是,如果python ORM类的名称与数据库列的名称不同(例如,在这3个ORM属性中): 那么这种方法就行不通了。 那么,如何获得pytho
那么,如何获得python版本的ORM属性呢?我定义了一个mixin类来增强
Base = declarative_base()
将mixin类定义为:
import inspect
import sqlalchemy as sqla
class orm_mixin_class(object):
"""Additional functionality wanted in orm classes (that inherit from Base)."""
@classmethod
def orm_class_info(cls):
"""
Get info about the orm class (as opposed to the database table it is mapped to).
Returns:
list of dict where each dict describes a orm class attribute.
Keys: s_class_attribute_name
"""
o_leaf_level_class = cls # this turns out the be the leaf class instead of this mixin class
l_orm_attribute_pairs = inspect.getmembers(o_leaf_level_class)
l_orm_class_info_dicts = []
for (s_class_attribute_name, o_attr_type) in l_orm_attribute_pairs:
d_attr_info = {}
b_orm_attribute = False
try:
o_inspect = sqla.inspection.inspect(o_attr_type)
if isinstance(o_inspect, sqla.orm.attributes.QueryableAttribute):
b_orm_attribute = True
d_attr_info['s_class_attribute_name'] = s_class_attribute_name
except:
pass
if b_orm_attribute:
# only orm attributes have an entry in the output list
l_orm_class_info_dicts.append(d_attr_info)
return(l_orm_class_info_dicts)
因此,可以通过方法调用轻松获得ORM属性列表
ORM类声明现在是:
class User(Base, orm_mixin_class):
我定义了一个mixin类来扩充
Base = declarative_base()
将mixin类定义为:
import inspect
import sqlalchemy as sqla
class orm_mixin_class(object):
"""Additional functionality wanted in orm classes (that inherit from Base)."""
@classmethod
def orm_class_info(cls):
"""
Get info about the orm class (as opposed to the database table it is mapped to).
Returns:
list of dict where each dict describes a orm class attribute.
Keys: s_class_attribute_name
"""
o_leaf_level_class = cls # this turns out the be the leaf class instead of this mixin class
l_orm_attribute_pairs = inspect.getmembers(o_leaf_level_class)
l_orm_class_info_dicts = []
for (s_class_attribute_name, o_attr_type) in l_orm_attribute_pairs:
d_attr_info = {}
b_orm_attribute = False
try:
o_inspect = sqla.inspection.inspect(o_attr_type)
if isinstance(o_inspect, sqla.orm.attributes.QueryableAttribute):
b_orm_attribute = True
d_attr_info['s_class_attribute_name'] = s_class_attribute_name
except:
pass
if b_orm_attribute:
# only orm attributes have an entry in the output list
l_orm_class_info_dicts.append(d_attr_info)
return(l_orm_class_info_dicts)
因此,可以通过方法调用轻松获得ORM属性列表
ORM类声明现在是:
class User(Base, orm_mixin_class):
这已通过以下方式实现:
这已通过以下方式实现:
属性列表可以使用.items()方法获得,因此我不必事先知道属性的名称:print(检查(python\u orm\u类).column\u attrs.items())关于如何通过检查获取信息的一个很好的概述:当我尝试这一点时,我得到了一个错误:ResultProxy类型的对象没有可用的检查系统。我们什么时候才能看到一种简单的内置方法来将ORM对象转换为dict(支持非循环递归关系等)?当使用Flask SQLAlchemy时,这将是一件很棒的事情:
返回jsonify([row.to_dict(),用于sqlalch_query.all()]中的row)
属性列表可以使用.items()方法使用,所以我不必事先知道属性的名称:print(检查(python_orm_class).column\u attrs.items())关于如何通过检查获取信息的一个很好的概述:当我尝试这一点时,我得到了一个错误:ResultProxy类型的对象没有可用的检查系统。我们什么时候才能看到一种简单的内置方法来将ORM对象转换为dict(支持非循环递归关系等)?当使用Flask SQLAlchemy时,这将是一件非常棒的事情:返回jsonify([row.to\u dict()表示sqlalch\u query.all()中的行)