SQLAlchemy如何设置hybrid_property的关键属性

时间:2014-06-07 06:12:40

标签: python sqlalchemy hybrid

我使用hybrid_property来组合或操纵列值。我希望得到一个关键的'此hybrid_property列的属性。这是代码。

class Foo(Base):

    name = Column(String)
    address = Column(String)
    city = Column(String)

    @hybrid_property
    def full_address(self):

        return '{}, {}'.format(self.address, self.city)

    @full_address.expression
    def full_address(self):

        return func.str('{}, {}'.format(self.address, self.city))

并在Python Shell中:

>>> fld = Foo.name
>>> fld.key
'name'
>>> fld = Foo.full_address
>>> fld.key
>>> type(fld.key)
<class 'NoneType'>

如何获取fld.key是&#39; full_address&#39;?

先谢谢。

1 个答案:

答案 0 :(得分:0)

最后我得到了答案

@hybrid_property def full_address(self):     if isinstance(self,Foo):         返回&#39; {},{}&#39; .format(self.address,self.city)     其他:         返回列(&#39; full_address&#39;,字符串)