pythonflask-sqlalchemyflask-admin

Flask-Admin: can a hybrid_property be searchable?


In a list view created with Flask-Admin, I have added two columns of an sqlalchemy object which are in fact computed in my object class using hybrid properties.


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)

So, I add them in the list view :


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'))
    [...]

And everything is ok except the fact that my two "hybrid columns" aren't searchable nor sortable. If I add them in column_searchable_list, I get :

'state' property :


    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' property :

    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.

The question is : Is it possible to use hybrid_property in this case or should I try a different solution ? If it's possible, I don't know for the moment how to solve these errors.


Solution

  • This was fixed recently in this pull request: https://github.com/flask-admin/flask-admin/pull/962

    Now, there's an example for using hybrid properties: https://github.com/flask-admin/flask-admin/tree/master/examples/sqla