flask-admin modelview 中重写get_query函数

背景:

flask-admin框架中提供的模型视图默认是显示表实体中的所有列表数据,如果想通过某种条件限制初始列表数据,那么久需要重写一些方法才能实现。

材料:

制作:

视图源码:

复制代码
    def get_query(self):
        return super(MyAiConfigView, self).get_query().filter(AiConfig.status == 0)

效果:

1、过滤前

2、过滤后

经验:

1、实现初始化过滤,我们可以想到的和百度上搜到的一定是重写def get_list(self, page, sort_field, sort_desc, search, filters, page_size=20) 来实现,这种方法的确可行,但这样处理不当会破坏原

复制代码
    # def get_list(self, page, sort_field, sort_desc, search, filters, page_size=20):
    #     query = self.get_query()
    #     count_query = self.get_count_query() if not self.simple_list_pager else None
    #     query = query.all()
    #     return None, query

始的配置项,这一点可以通过源码看出

复制代码
                 execute=True, page_size=None):
        """
            Return records from the database.

            :param page:
                Page number
            :param sort_column:
                Sort column name
            :param sort_desc:
                Descending or ascending sort
            :param search:
                Search query
            :param execute:
                Execute query immediately? Default is `True`
            :param filters:
                List of filter tuples
            :param page_size:
                Number of results. Defaults to ModelView's page_size. Can be
                overriden to change the page_size limit. Removing the page_size
                limit requires setting page_size to 0 or False.
        """

        # Will contain join paths with optional aliased object
        joins = {}
        count_joins = {}

        query = self.get_query()
        count_query = self.get_count_query() if not self.simple_list_pager else None

        # Ignore eager-loaded relations (prevent unnecessary joins)
        # TODO: Separate join detection for query and count query?
        if hasattr(query, '_join_entities'):
            for entity in query._join_entities:
                for table in entity.tables:
                    joins[table] = None

        # Apply search criteria
        if self._search_supported and search:
            query, count_query, joins, count_joins = self._apply_search(query,
                                                                        count_query,
                                                                        joins,
                                                                        count_joins,
                                                                        search)

        # Apply filters
        if filters and self._filters:
            query, count_query, joins, count_joins = self._apply_filters(query,
                                                                         count_query,
                                                                         joins,
                                                                         count_joins,
                                                                         filters)

        # Calculate number of rows if necessary
        count = count_query.scalar() if count_query else None

        # Auto join
        for j in self._auto_joins:
            query = query.options(joinedload(j))

        # Sorting
        query, joins = self._apply_sorting(query, joins, sort_column, sort_desc)

        # Pagination
        query = self._apply_pagination(query, page, page_size)

        # Execute if needed
        if execute:
            query = query.all()

        return count, query

2、在源代码学习中发现了本文探讨的新大陆---get_query(self) ,下面是源码中针对该函数的例子

复制代码
                class MyView(ModelView):
                    def get_query(self):
                        return super(MyView, self).get_query().filter(User.username == current_user.username)

经过实地验证,效果理想

相关推荐
Java 码农18 分钟前
Centos7 maven 安装
java·python·centos·maven
你的人类朋友1 小时前
先用js快速开发,后续引入ts是否是一个好的实践?
前端·javascript·后端
倔强青铜三1 小时前
苦练Python第63天:零基础玩转TOML配置读写,tomllib模块实战
人工智能·python·面试
浔川python社1 小时前
《网络爬虫技术规范与应用指南系列》(xc—3):合规实操与场景落地
python
码事漫谈1 小时前
医疗设备控制系统中同步与异步通信的架构设计
后端
B站计算机毕业设计之家1 小时前
智慧交通项目:Python+YOLOv8 实时交通标志系统 深度学习实战(TT100K+PySide6 源码+文档)✅
人工智能·python·深度学习·yolo·计算机视觉·智慧交通·交通标志
码事漫谈1 小时前
C++ 中 rfind 方法详解
后端
IT森林里的程序猿2 小时前
基于机器学习方法的网球比赛胜负趋势预测
python·机器学习·django
正牌强哥2 小时前
Futures_ML——机器学习在期货量化交易中的应用与实践
人工智能·python·机器学习·ai·交易·akshare
倔强青铜三2 小时前
苦练Python第62天:零基础玩转CSV文件读写,csv模块实战
人工智能·python·面试