gpt4 book ai didi

python - Flask-Admin:可以搜索 hybrid_property 吗?

转载 作者:行者123 更新时间:2023-11-28 16:36:26 24 4
gpt4 key购买 nike

在使用 Flask-Admin 创建的 ListView 中,我添加了一个 sqlalchemy 对象的两列,它们实际上是在我的对象类中使用混合属性计算的。


class MyClass(db.Model):

@hybrid_property
def state(self):
now = datetime.datetime.now()
state = 'OK'
for panne in self.pannes:
if panne.f_indispo < now:
continue
if panne.d_indispo <= now and panne.f_indispo >= now:
state = 'KO'
break
if panne.d_indispo > now:
state = 'PR'
return state

@hybrid_property
def station_name(self):
if app.config['STATIONS'] is not None and self.id_station in app.config['STATIONS']:
return app.config['STATIONS'][self.id_station]
return unicode(self.id_station)

所以,我将它们添加到 ListView 中:


class MyClassView(ModelView):
list_template = 'some/template.html'
column_list = ('column1','column2','state','station_name')
column_searchable_list = ('column1','column2')
column_formatters = dict(state=macro('macro_in_my_template'))
[...]

一切正常,除了我的两个“混合列”不可搜索也不可排序。如果我将它们添加到 column_searchable_list 中,我会得到:

'状态'属性:

    File "/usr/lib/python2.6/site-packages/Flask_Admin-1.0.8-py2.6.egg/flask_admin/contrib/sqla/view.py", line 271, in __init__        super(ModelView, self).__init__(model, name, category, endpoint, url)    File "/usr/lib/python2.6/site-packages/Flask_Admin-1.0.8-py2.6.egg/flask_admin/model/base.py", line 515, in __init__        self._refresh_cache()    File "/usr/lib/python2.6/site-packages/Flask_Admin-1.0.8-py2.6.egg/flask_admin/model/base.py", line 540, in _refresh_cache        self._search_supported = self.init_search()    File "/usr/lib/python2.6/site-packages/Flask_Admin-1.0.8-py2.6.egg/flask_admin/contrib/sqla/view.py", line 410, in init_search        for column in self._get_columns_for_field(p):    File "/usr/lib/python2.6/site-packages/Flask_Admin-1.0.8-py2.6.egg/flask_admin/contrib/sqla/view.py", line 379, in _get_columns_for_field        attr = getattr(self.model, field, None)    File "/usr/lib/python2.6/site-packages/SQLAlchemy-0.9.6-py2.6-linux-x86_64.egg/sqlalchemy/ext/hybrid.py", line 738, in __get__        return self.expr(owner)    File "/var/www/***/app/***.py", line 90, in state        for panne in self.pannes:    File "/usr/lib/python2.6/site-packages/SQLAlchemy-0.9.6-py2.6-linux-x86_64.egg/sqlalchemy/sql/operators.py", line 338, in __getitem__        return self.operate(getitem, index)    File "/usr/lib/python2.6/site-packages/SQLAlchemy-0.9.6-py2.6-linux-x86_64.egg/sqlalchemy/orm/attributes.py", line 171, in operate        return op(self.comparator, *other, **kwargs)    File "/usr/lib/python2.6/site-packages/SQLAlchemy-0.9.6-py2.6-linux-x86_64.egg/sqlalchemy/sql/operators.py", line 338, in __getitem__        return self.operate(getitem, index)    File "/usr/lib/python2.6/site-packages/SQLAlchemy-0.9.6-py2.6-linux-x86_64.egg/sqlalchemy/sql/operators.py", line 183, in operate        raise NotImplementedError(str(op))        NotImplementedError: 

'station_name' 属性:

    File "/usr/lib/python2.6/site-packages/Flask_Admin-1.0.8-py2.6.egg/flask_admin/contrib/sqla/view.py", line 271, in __init__        super(ModelView, self).__init__(model, name, category, endpoint, url)    File "/usr/lib/python2.6/site-packages/Flask_Admin-1.0.8-py2.6.egg/flask_admin/model/base.py", line 515, in __init__        self._refresh_cache()    File "/usr/lib/python2.6/site-packages/Flask_Admin-1.0.8-py2.6.egg/flask_admin/model/base.py", line 540, in _refresh_cache        self._search_supported = self.init_search()    File "/usr/lib/python2.6/site-packages/Flask_Admin-1.0.8-py2.6.egg/flask_admin/contrib/sqla/view.py", line 410, in init_search        for column in self._get_columns_for_field(p):    File "/usr/lib/python2.6/site-packages/Flask_Admin-1.0.8-py2.6.egg/flask_admin/contrib/sqla/view.py", line 390, in _get_columns_for_field        raise Exception('Invalid field %s: does not contains any columns.' % field)        Exception: Invalid field station_name: does not contains any columns.

问题是:在这种情况下可以使用 hybrid_property 还是我应该尝试不同的解决方案?如果可能的话,我暂时不知道如何解决这些错误。

最佳答案

最近在此拉取请求中修复了此问题:https://github.com/flask-admin/flask-admin/pull/962

现在,有一个使用混合属性的例子:https://github.com/flask-admin/flask-admin/tree/master/examples/sqla

关于python - Flask-Admin:可以搜索 hybrid_property 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25644813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com