1

我正在为我们的前端开发人员创建 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:这个烧瓶插件工作正常,模型中没有混合属性,但混合列对我的项目很重要

4

1 回答 1

0

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

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

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

可能的解决方案:

  1. 设置对 Flask-Restless 的 master 分支的依赖。这个解决方案不是很好的 IMO,因为它有点临时(未来的更改可能会破坏您的代码)。
  2. 分叉 repo 并实施原始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")
    
于 2020-03-02T10:29:12.690 回答