DateBetweenFilter过滤器不工作在烧瓶管理员 [英] DateBetweenFilter filter is not working in flask admin

查看:291
本文介绍了DateBetweenFilter过滤器不工作在烧瓶管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在烧瓶管理员中创建了一个自定义 datetimefilter ,它在UI中看起来不错(可以选择datetime范围),但不能在功能上工作。我认为apply方法不工作。我将应用此过滤器的表位于 Cassandra

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

和使用过滤器的相应Admin类

and the corresponding Admin class that uses the filter

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

我遗漏了什么?

推荐答案

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

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

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

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)

这篇关于DateBetweenFilter过滤器不工作在烧瓶管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆