gpt4 book ai didi

python - 从 SQLAlchemy 中的另一列创建一列

转载 作者:太空狗 更新时间:2023-10-30 01:01:27 26 4
gpt4 key购买 nike

我想要一个 SQLAlchemy Column 可以从另一列计算。例如,列 month ({'Jan', ...}) 来自 date 列(如 2014-09-12 ),所以从技术上讲,它是从 09Sep 的一次性解析。

是否可以将其写入对象本身的类中?或者只是在每次记录更新时运行更新?

PS:以月份为例。我对派生列的空间无限大的一般情况感兴趣。

最佳答案

column_property可能会满足您的需求。这允许您创建一些外观和行为类似于普通映射对象列的东西,但它是根据其他列自动计算的。

Example:

class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)

# actual database columns
firstname = Column(String(50))
lastname = Column(String(50))

# calculated from the other two
fullname = column_property(firstname + " " + lastname)

在您的示例用法中:

class Foo(Base):
date = Column(DateTime)
month_only = column_property(date.strptime("%b")) # 'Jan','Feb',etc.

关于python - 从 SQLAlchemy 中的另一列创建一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25816255/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com