在我的模型中,烧瓶不安分不适用于混合属性



我正在为我们的前端开发人员创建api,但我的模型中的对象有问题。我使用flask-sqlalchemypython2.7

manager = flask_restless.APIManager(app, session = s)
manager.create_api(Basic_Abones)

ORM模型

class Basic_Abones(Base):
__tablename__ = 'basic_abones'
id = Column(Integer, primary_key=True)
name = Column(String(120))
part = Column(Integer)
current_price = Column(Integer)
complex_a = relationship("Complex_Abon", secondary=assoc, back_populates='basic')
discount = Column(Integer)

def __repr__(self):
return self.name
@hybrid_property
def price_with_discount(self):
try:
return self.current_price * self.discount / 100
except TypeError:
return 0 

例外

127.0.0.1 - - [09/Jan/2018 13:10:04] "GET / HTTP/1.1" 200 -
--------------------------------------------------------------------------------
ERROR in views [/home/yozh/AllStars/allstars/local/lib/python2.7/site-packages/flask_restless/views.py:1178]:
Neither 'hybrid_property' object nor 'ExprComparator' object associated with Basic_Abones.price_with_discount has an attribute 'property'
--------------------------------------------------------------------------------
Traceback (most recent call last):
File "/home/yozh/AllStars/allstars/local/lib/python2.7/site-packages/flask_restless/views.py", line 1172, in _search
result = search(self.session, self.model, search_params)
File "/home/yozh/AllStars/allstars/local/lib/python2.7/site-packages/flask_restless/search.py", line 587, in search
query = create_query(session, model, search_params, _ignore_order_by)
File "/home/yozh/AllStars/allstars/local/lib/python2.7/site-packages/flask_restless/search.py", line 549, in create_query
_ignore_order_by)
File "/home/yozh/AllStars/allstars/local/lib/python2.7/site-packages/flask_restless/search.py", line 505, in create_query
pks = primary_key_names(model)
File "/home/yozh/AllStars/allstars/local/lib/python2.7/site-packages/flask_restless/helpers.py", line 216, in primary_key_names
and isinstance(field.property, ColumnProperty)
File "/home/yozh/AllStars/allstars/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 314, in __getattr__
attribute)
AttributeError: Neither 'hybrid_property' object nor 'ExprComparator' object associated with Basic_Abones.price_with_discount has an attribute 'property'
127.0.0.1 - - [09/Jan/2018 13:10:14] "GET /api/basic_abones HTTP/1.1" 400 -

我不知道会发生什么,我在谷歌上找不到解决的问题。 谢谢你,对不起我的英语

PS:这个烧瓶插件在模型中没有混合属性的情况下工作正常,但混合列对我的项目很重要

看起来,这是Flask-Restless项目中的一个已知错误,而不是在Flask-sqlalchemy中。

它已经修复在主分支上,但没有实现该解决方案的当前稳定版本。

问题的根本原因在于flask-restless 的 helpers.py 文件中,在尝试访问hybrid_property对象的名为 property 的字段的"primary_key_names"中。由于它不存在,因此会引发异常。

可能的解决方案:

  1. 设置对 Flask-Restless 主分支的依赖关系。这个解决方案不是那么好的IMO,因为它有点临时(未来的更改可能会破坏你的代码)。
  2. 分叉存储库并实现原始 github 问题中提出的解决方案。
  3. 选择在我这边实现的解决方案是在我的代码上解决该问题:如果对缺少属性属性的访问导致异常,让我们在hybrid_propery上设置属性字段,如下所示:

    @hybrid_property
    def key_id(self):
    return int(self.key.split('-')[1])
    @key_id.expression
    def key_id(cls):
    return cast(func.split_part(cls.key, '-', 2), Integer)
    key_id.__setattr__("property", "None")
    

相关内容

  • 没有找到相关文章

最新更新