gpt4 book ai didi

python - SQLAlchemy ORM : create 2 attributes for the same column

转载 作者:行者123 更新时间:2023-12-01 09:06:44 26 4
gpt4 key购买 nike

我正在重构遗留的 Alchemy ORM 项目,出于复古兼容性的原因,我需要保留一些旧的属性名称并使用新名称重新映射它们。

Alchemy ORM 提供column_property(attribute) 函数,帮助创建引用其他属性的只读新属性。解决此问题的另一种已知方法是使用hybrid_property:

@hybrid_property
def price(self):
return self.old_price

@price.setter
def price(self, value):
self.old_price = value

是否有更简洁的方法来为列的别名 getter和setter。

最佳答案

对于简单的镜像SQLAlchemy提供synonym() :

class Foo(Base):
old_price = Column(...)
price = synonym("old_price")

关于python - SQLAlchemy ORM : create 2 attributes for the same column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51981733/

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