检查属性是否是 SqlAlchemy Hybrid_property

问题描述 投票:0回答:1

我有一个类似于下面的 mixin,我需要区分

normal_relationship
attr 和
special
attr。

class MyMixin(object):

  @declared_attr
  def normal_relationship(self):
    return relationship(
        "CustomAttributeValue",
        primaryjoin=my_good_join_function,
        backref='{0}_something_else'.format(self.__name__),
        cascade='all, delete-orphan',
    )

  @declared_attr
  def _special_relationship(self):
    return relationship(
        "CustomAttributeValue",
        primaryjoin=my_good_join_function,
        backref='{0}_something'.format(self.__name__),
        cascade='all, delete-orphan',
    )

  @hybrid_property
  def special(self):
    return self._special_relationship

  @special.setter
  def special(self, values):
    self._special_relationship.append(value*2)

因为

special
的 getter 返回
_special_relationship
,它与
normal_relationship
相同,我不知道如何检查这种差异。

getattr(MyMixin, "special")
getattr(MyMixin, "_special_relationship")
相同。

有什么办法,或者我可以向特殊的混合属性添加任何内容来检查它是否有自定义设置器。

基本上弄清楚哪些属性定义了特殊的设置器会非常好。

python python-2.7 sqlalchemy
1个回答
0
投票

这个对象有一个属性,名为“描述符”

type(model_attr.descriptor)
>>> <class 'sqlalchemy.ext.hybrid.hybrid_property>

所以,试试这个:

from sqlqalchemy.exthybrid import hybrid_property
    
model_attr = getattr(MyMixin, "special")
if isinstance(getattr(model_attr, 'descriptor', None), hybrid_property):
    # do your magic
© www.soinside.com 2019 - 2024. All rights reserved.