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)

经过实地验证,效果理想

相关推荐
程序员海军2 小时前
用了两年多 GPT,我最近开始换模型了,还折腾了个求职避坑 Agent
前端·人工智能·后端
码士集团小青2 小时前
SpringBoot 集成缓存性能之王 Caffeine
spring boot·后端·缓存
excel4 小时前
IndexNow:让搜索引擎更快发现网站更新的主动通知协议
前端·后端·架构
Json____4 小时前
用 FastAPI + Vue3 打造一套完整的在线考试系统:从组卷到阅卷的业务闭环实践
后端·node.js·fastapi·it学习·wwwoop.com
小灰灰搞电子5 小时前
Rust+Slint 实现FTP客户端调试助手源码分享
开发语言·后端·rust
外收内放5 小时前
面向对象高级(实战案例)
开发语言·python·学习
专业程序开发源6 小时前
springboot欧缇蔓月子中心管理系统82697-计算机课程设计、毕业设计
java·spring boot·后端·python·spring·django·课程设计
FPGA小迷弟6 小时前
用 Claude Code 重构遗留系统:从评估到落地的完整实践指南
c++·python·ai·plotly·django·ai编程·claude
卷无止境6 小时前
面向arm64桌面客户端开发全景:语言、框架与生态
后端·python
fanruitian6 小时前
python else用法
开发语言·python