DateBetweenFilter filter is not working in flask admin

▼魔方 西西 提交于 2019-12-24 12:51:36

问题


I created a custom datetimefilter in flask admin and it looks good(able to select the datetime range) in the UI, but not working in functionality. I think the apply method is not working. The table which i'm going to apply this filter is in 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))

and the corresponding Admin class that uses the filter

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

what I'm missing?


回答1:


You may missed to add the code that invokes the apply() for the filters defined in the admin class in the get_list() method.

Try to add the below code in your get_list() method.

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)


来源:https://stackoverflow.com/questions/36067581/datebetweenfilter-filter-is-not-working-in-flask-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!