当sqlalchemy ORM类属性与数据库列不同时,如何获取ORM类属性列表?

时间:2015-01-14 16:04:11

标签: python sqlalchemy

假设您要在sqlalchemy中迭代ORM类的ORM属性。 因此,您需要一个ORM属性列表。你如何得到这份名单?

如果ORM类没有重命名属性,因此ORM属性与数据库列匹配,那么您可以使用以下解决方案: https://stackoverflow.com/a/24748320/1023033 (顺便说一下,源代码文件/lib/sqlalchemy/orm/base.py中还有一个内置(私有)函数_orm_columns(),似乎提供了这个功能)

但是如果python ORM类的名称与数据库列的名称不同(例如在这3个ORM属性中):

>>> class User(Base):
...     __tablename__ = 'users'
...
...     id = Column('pkey', Integer, primary_key=True)
...     name = Column('user_name', String)
...     fullname = Column('human_name', String)

然后该方法不起作用。 那么,你如何获得ORM属性的python版本?

2 个答案:

答案 0 :(得分:4)

这已经使用inspection system

实现
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import inspect

Base = declarative_base()

class A(Base):
    __tablename__ = 'a'
    id = Column(Integer, primary_key=True)
    x = Column(Integer)
    y = Column(Integer)

print inspect(A).c
print inspect(A).c.x
print inspect(A).column_attrs
print inspect(A).column_attrs.x
print inspect(A).column_attrs.x.expression

http://docs.sqlalchemy.org/en/latest/core/inspection.html

http://docs.sqlalchemy.org/en/latest/orm/mapping_api.html#sqlalchemy.orm.mapper.Mapper.all_orm_descriptors

http://docs.sqlalchemy.org/en/latest/orm/mapping_api.html#sqlalchemy.orm.mapper.Mapper.columns

http://docs.sqlalchemy.org/en/latest/orm/mapping_api.html#sqlalchemy.orm.mapper.Mapper.column_attrs

http://docs.sqlalchemy.org/en/latest/orm/mapping_api.html#sqlalchemy.orm.mapper.Mapper.c

答案 1 :(得分:0)

我定义了一个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):