Flask-Admin:可以搜索hybrid_property吗?

时间:2014-09-03 12:49:58

标签: python flask-sqlalchemy flask-admin

在使用Flask-Admin创建的列表视图中,我添加了两列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)

所以,我在列表视图中添加它们:


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或者我应该尝试不同的解决方案? 如果有可能,我暂时不知道如何解决这些错误。

1 个答案:

答案 0 :(得分:3)

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

现在,有一个使用混合属性的示例:https://github.com/flask-admin/flask-admin/blob/master/examples/sqla-hybrid_property/app.py