flask-restless不适用于我的模型中的混合属性

时间:2018-01-09 11:31:02

标签: python flask sqlalchemy flask-sqlalchemy flask-restless

我为我们的前端开发人员创建了api,我的模型中的对象有问题。我使用 flask-sqlalchemy python2.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:这个烧瓶插件工作正常,没有模型中的混合属性,但混合列对我的项目很重要

1 个答案:

答案 0 :(得分:0)

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

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

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

可能的解决方案:

  1. 在Flask-Restless的master分支上设置依赖项。这种解决方案并不是很好的IMO,因为它只是暂时的(将来的更改可能会破坏您的代码)。
  2. 分叉存储库并实施原始github issue中提出的解决方案。
  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")