2014-09-03 96 views
4

在使用Flask-Admin創建的列表視圖中,我添加了兩列sqlalchemy對象,這些列實際上是在我的對象類中使用混合屬性進行計算的。Flask-Admin:可以搜索hybrid_property嗎?


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: 

'站的班次' 屬性:

 
    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在這種情況下還是應該嘗試一種不同的解決方案? 如果可能的話,我現在不知道如何解決這些錯誤。

回答

相關問題