DateBetweenFilter过滤器在flask admin

时间:2016-03-17 17:22:17

标签: filter flask cassandra flask-admin

我在flask admin中创建了一个自定义datetimefilter,它在UI中看起来不错(能够选择日期时间范围),但在功能方面却没有。我认为apply方法不起作用。我将要应用此过滤器的表位于Cassandra

from flask_admin.contrib.sqla.filters import BaseSQLAFilter
from flask_admin.model import filters
class DateBetweenFilter(BaseSQLAFilter, filters.BaseDateBetweenFilter):
    def __init__(self, column, name, options=None, data_type=None):
        super(DateBetweenFilter, self).__init__(column,
                                                name,
                                                options,
                                                data_type='daterangepicker')

def apply(self, query, value, alias=None):
    start, end = value
    return query.filter(self.get_column(alias).between(start, end))

以及使用过滤器的相应Admin类

class SearchAdminView(BaseModelView):
    column_filters = [
      DateBetweenFilter(
         Search.created_date, 'Created Date'
      )
    ]

我缺少什么?

1 个答案:

答案 0 :(得分:1)

您可能错过了为get_list()方法中的admin类中定义的过滤器添加调用apply()的代码。

尝试在get_list()方法中添加以下代码。

def get_list(self, page, sort_column, sort_desc, search, filters,
             execute=True, page_size=None):
    """
        Get list of objects
    """
    query = self.get_query(page, page_size)
    for flt, k, value in filters:
        query = self._filters[flt].apply(query, value, page_size)