Search code examples
filterflaskcassandraflask-admin

DateBetweenFilter filter is not working in flask admin


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?


Solution

  • 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)