python - SQLALchemy 访问列对象

标签 python python-3.x sqlalchemy

我正在尝试找到解决 SQLAlchemy 的方法。我正在尝试找出如何通过表实例访问 Column 对象。

我目前使用 Peewee 作为 ORM 编写了我的应用程序,我可以执行以下操作:

In [15]:from db.models.vehicle import Vehicle
In [16]: Vehicle.code
Out[16]: <db.fields.RsCharField at 0x7fd93e53e780>
In [17]: Vehicle.code.default
Out[17]: ''

或者通过实际实例:

In [10]: a = Vehicle()
In [11]: a._meta.fields["code"]
Out[11]: <db.fields.RsCharField at 0x7f08ce49da90>

当我在 SQLAlchemy 中执行相同操作时,我得到一个:

In [11]: Vehicle.code
Out[11]: <sqlalchemy.orm.attributes.InstrumentedAttribute at 0x7fd9105f2938>

但我想要拥有实际的列类型,因为我扩展了列类型以具有我想要访问的额外属性:

class RsColumn(Column):

    def __init__(self, *args, ui_group=None, ui_sequence=None, **kwargs):
        self.ui_group = ui_group
        self.ui_sequence = ui_sequence
        super().__init__(*args, **kwargs)

(所以在这种情况下,我想访问我在表声明中使用的 RsColumn 对象)

最佳答案

Vehicle.__table__.c.code.type

您可以在 SQLAlchemy Documentation: Accessing Tables and Columns 中阅读相关内容

关于python - SQLALchemy 访问列对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41097667/

相关文章:

python - 使用 Pandas 在另一列中查找列字符串的位置

python - 更改Python的日期时间模块中的年份开始时间?

Python PLY Lex 歧义

python - 无法解封

python - 将 pdf 文件附加到 EmailMessage?

python - 从 subprocess.Popen 进行流式传输并使用两个子命令时出现死锁

python - sqlalchemy postgresql 其中 int = 字符串

python - 使用 session.query 通过 SQLAlchemy ORM 更新连接表

python - 使用 Postgres 和 SQLAlchemy 过滤数组列

python - 在图像中插入随机零像素