odoo17核心概念view5——ir_ui_view.py

news2025/2/7 2:16:03

这是view系列的第5篇文章,介绍一下view对应的后端文件ir_ui_view.py,它是base模块下的一个文件
位置:odoo\addons\base\models\ir_ui_view.py

该文件一共定义了三个模型

1.1 ir.ui.view.custom

查询数据库这个表是空的,从名字看和数据库表结构看, 这个表应该是view和user的三方表,可以根据用户自定义view, 但是案例呢?

class ViewCustom(models.Model):
    _name = 'ir.ui.view.custom'
    _description = 'Custom View'
    _order = 'create_date desc'  # search(limit=1) should return the last customization
    _rec_name = 'user_id'

    ref_id = fields.Many2one('ir.ui.view', string='Original View', index=True, required=True, ondelete='cascade')
    user_id = fields.Many2one('res.users', string='User', index=True, required=True, ondelete='cascade')
    arch = fields.Text(string='View Architecture', required=True)

    def _auto_init(self):
        res = super(ViewCustom, self)._auto_init()
        tools.create_index(self._cr, 'ir_ui_view_custom_user_id_ref_id',
                           self._table, ['user_id', 'ref_id'])
        return res

1.2 ir.ui.view

截取一部分字段, view的字典表,保存从xml中解析的view信息。


class View(models.Model):
    _name = 'ir.ui.view'
    _description = 'View'
    _order = "priority,name,id"

    name = fields.Char(string='View Name', required=True)
    model = fields.Char(index=True)
    key = fields.Char(index='btree_not_null')
    priority = fields.Integer(string='Sequence', default=16, required=True)
    type = fields.Selection([('tree', 'Tree'),
                             ('form', 'Form'),
                             ('graph', 'Graph'),
                             ('pivot', 'Pivot'),
                             ('calendar', 'Calendar'),
                             ('gantt', 'Gantt'),
                             ('kanban', 'Kanban'),
                             ('search', 'Search'),
                             ('qweb', 'QWeb')], string='View Type')
    arch = fields.Text(compute='_compute_arch', inverse='_inverse_arch', string='View Architecture',
                       help="""This field should be used when accessing view arch. It will use translation.
                               Note that it will read `arch_db` or `arch_fs` if in dev-xml mode.""")
    arch_base = fields.Text(compute='_compute_arch_base', inverse='_inverse_arch_base', string='Base View Architecture',
                            help="This field is the same as `arch` field without translations")
    arch_db = fields.Text(string='Arch Blob', translate=xml_translate,
                          help="This field stores the view arch.")
    arch_fs = fields.Char(string='Arch Filename', help="""File from where the view originates.
                                                          Useful to (hard) reset broken views or to read arch from file in dev-xml mode.""")

1.3 Model

这是一个抽象模型,

class Model(models.AbstractModel):
    _inherit = 'base'

    _date_name = 'date'         #: field to use for default calendar view

它继承自base模型,让我们看看base模型是何方神圣,从注释看,这是一个基本模型,所有的模型都要继承它。
odoo\addons\base\models\ir_model.py
这是一个抽象模型,抽象的只有一名字而已。估计又很多地方对这个模型进行了扩展。我们搜索一下

#
# IMPORTANT: this must be the first model declared in the module
#
class Base(models.AbstractModel):
    """ The base model, which is implicitly inherited by all models. """
    _name = 'base'
    _description = 'Base'

在这里插入图片描述
前端的viewService中通过orm调用的getViews就是最终就是调用了这个模型的get_views方法

    @api.model
    def get_views(self, views, options=None):
        """ Returns the fields_views of given views, along with the fields of
        the current model, and optionally its filters for the given action.

        The return of the method can only depend on the requested view types,
        access rights (views or other records), view access rules, options,
        context lang and TYPE_view_ref (other context values cannot be used).

        Python expressions contained in views or representing domains (on
        python fields) will be evaluated by the client with all the context
        values as well as the record values it has.

        :param views: list of [view_id, view_type]
        :param dict options: a dict optional boolean flags, set to enable:

            ``toolbar``
                includes contextual actions when loading fields_views
            ``load_filters``
                returns the model's filters
            ``action_id``
                id of the action to get the filters, otherwise loads the global
                filters or the model

        :return: dictionary with fields_views, fields and optionally filters
        """

调用链条:
get_views => get_view => _get_view_cache => _get_view
重点看一下 _get_view这个私有函数:

 @api.model
    def _get_view(self, view_id=None, view_type='form', **options):
        """Get the model view combined architecture (the view along all its inheriting views).

        :param int view_id: id of the view or None
        :param str view_type: type of the view to return if view_id is None ('form', 'tree', ...)
        :param dict options: bool options to return additional features:
            - bool mobile: true if the web client is currently using the responsive mobile view
              (to use kanban views instead of list views for x2many fields)
        :return: architecture of the view as an etree node, and the browse record of the view used
        :rtype: tuple
        :raise AttributeError:
            if no view exists for that model, and no method `_get_default_[view_type]_view` exists for the view type

        """
        View = self.env['ir.ui.view'].sudo()

        # try to find a view_id if none provided
        if not view_id:
            # <view_type>_view_ref in context can be used to override the default view
            view_ref_key = view_type + '_view_ref'
            view_ref = self._context.get(view_ref_key)
            if view_ref:
                if '.' in view_ref:
                    module, view_ref = view_ref.split('.', 1)
                    query = "SELECT res_id FROM ir_model_data WHERE model='ir.ui.view' AND module=%s AND name=%s"
                    self._cr.execute(query, (module, view_ref))
                    view_ref_res = self._cr.fetchone()
                    if view_ref_res:
                        view_id = view_ref_res[0]
                else:
                    _logger.warning(
                        '%r requires a fully-qualified external id (got: %r for model %s). '
                        'Please use the complete `module.view_id` form instead.', view_ref_key, view_ref,
                        self._name
                    )

            if not view_id:
                # otherwise try to find the lowest priority matching ir.ui.view
                view_id = View.default_view(self._name, view_type)

        if view_id:
            # read the view with inherited views applied
            view = View.browse(view_id)
            arch = view._get_combined_arch()
        else:
            # fallback on default views methods if no ir.ui.view could be found
            view = View.browse()
            try:
                arch = getattr(self, '_get_default_%s_view' % view_type)()
            except AttributeError:
                raise UserError(_("No default view of type '%s' could be found!", view_type))
        return arch, view

如果没有传入view_id, 那么就去上下文中查找_view_ref, 这给了我们一种思路,那就是可以在上下文中指定视图。
如果还是获取不到view_id,那么调用View.default_view函数获取默认的视图。
如果获取到了view_id, view返回该条记录,而arch返回处理好继承关系之后的结构

            view = View.browse(view_id)
            arch = view._get_combined_arch()

如果view_id 还是没有获取到,那么还是做了最后的尝试

arch = getattr(self, '_get_default_%s_view' % view_type)()

调用了视图类型对应的函数,以form为例,调用_get_default_form_view返回arch,如果没有这个函数,那就抛出异常。 odoo: 我已经尽了我最大的努力了。。。

1.4 view_ref 应用的案例

<page string="Analytic Lines" name="analytic_lines" groups="analytic.group_analytic_accounting">
   <field name="date" invisible="1"/>
   <field name="analytic_line_ids" context="{'tree_view_ref':'analytic.view_account_analytic_line_tree', 'default_general_account_id':account_id, 'default_name': name, 'default_date':date, 'amount': (debit or 0.0)-(credit or 0.0)}"/>
</page>

随便找了 一个例子,这是一个x2many字段,在context中指定了 tree_view_ref,那么系统在打开的时候就会调用这里指定的tree视图,而不是默认的tree视图。

ir_ui_view.py的代码有3000行,这里只介绍了很少的一部分,后面如果有其他的知识点,再来补充。

1.5 _get_default_form_view

通过代码动态生成视图,这给了我们一共思路,就是在审批流中等需要动态生成视图的场景下,可以参考这种方式。
本质是动态生成或者改变arch,在后端或者前端都可以做。


    @api.model
    def _get_default_form_view(self):
        """ Generates a default single-line form view using all fields
        of the current model.

        :returns: a form view as an lxml document
        :rtype: etree._Element
        """
        sheet = E.sheet(string=self._description)
        main_group = E.group()
        left_group = E.group()
        right_group = E.group()
        for fname, field in self._fields.items():
            if field.automatic:
                continue
            elif field.type in ('one2many', 'many2many', 'text', 'html'):
                # append to sheet left and right group if needed
                if len(left_group) > 0:
                    main_group.append(left_group)
                    left_group = E.group()
                if len(right_group) > 0:
                    main_group.append(right_group)
                    right_group = E.group()
                if len(main_group) > 0:
                    sheet.append(main_group)
                    main_group = E.group()
                # add an oneline group for field type 'one2many', 'many2many', 'text', 'html'
                sheet.append(E.group(E.field(name=fname)))
            else:
                if len(left_group) > len(right_group):
                    right_group.append(E.field(name=fname))
                else:
                    left_group.append(E.field(name=fname))
        if len(left_group) > 0:
            main_group.append(left_group)
        if len(right_group) > 0:
            main_group.append(right_group)
        sheet.append(main_group)
        sheet.append(E.group(E.separator()))
        return E.form(sheet)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1336536.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于ssm学生考勤管理系统的设计与实现论文

目 录 目 录 I 摘 要 III ABSTRACT IV 1 绪论 1 1.1 课题背景 1 1.2 研究现状 1 1.3 研究内容 2 2 系统开发环境 3 2.1 vue技术 3 2.2 JAVA技术 3 2.3 MYSQL数据库 3 2.4 B/S结构 4 2.5 SSM框架技术 4 3 系统分析 5 3.1 可行性分析 5 3.1.1 技术可行性 5 3.1.2 操作可行性 5 3…

每日一练2023.12.25——验证身份【PTA】

题目链接 &#xff1a;验证身份 题目要求&#xff1a; 一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下&#xff1a; 首先对前17位数字加权求和&#xff0c;权重分配为&#xff1a;{7&#xff0c;9&#xff0c;10&#xff0c;5&a…

C语言操作符详情

C语言操作符详情 是否控制求值顺序中 只有“&&”“&#xff1f;&#xff01;”“&#xff0c;”“||”为“是” 其余均为“否”

AI+CRM能做什么?人工智能在客户管理系统中的应用

Hello&#xff0c;大家好&#xff0c;今天小编和大家分享AI人工智能在CRM系统中的应用。运用AI的场景包括&#xff1a;赋能内容生产、客户服务支持、赋能品牌推广、自动化业务流程、数据分析、辅助科学决策、给出最佳客户联系时间。合理运用CRM系统中AI人工智能助手可以让团队工…

RetinaNet:Focal Loss for Dense Object Detection(CVPR2018)

文章目录 Abstract北京发现问题并给出方法成果 IntroductionRelated WorkRobust 评估 Focal LossBalanced Cross EntropyFocal Loss DefinitionClass Imbalance and Model InitializationClass Imbalance and Two-stage Detectors RetinaNet DetectorExperimentsConclusion hh …

BFS解决多源最短路相关leetcode算法题

文章目录 1.01矩阵2.飞地的数量3.地图中的最高点4.地图分析 1.01矩阵 01矩阵 class Solution {int dx[4] {0,0,1,-1};int dy[4] {1,-1,0,0}; public:vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {//正难则反&#xff0c;找0…

基于机器学习算法的数据分析师薪资预测模型优化研究(文末送书)

&#x1f935;‍♂️ 个人主页&#xff1a;艾派森的个人主页 ✍&#x1f3fb;作者简介&#xff1a;Python学习者 &#x1f40b; 希望大家多多支持&#xff0c;我们一起进步&#xff01;&#x1f604; 如果文章对你有帮助的话&#xff0c; 欢迎评论 &#x1f4ac;点赞&#x1f4…

【WordPress插件】热门关键词推荐v1.3.0 Pro开心版

介绍&#xff1a; WordPress插件-WBOLT热门关键词推荐插件&#xff08;Smart Keywords Tool&#xff09;是一款集即时关键词推荐、关键词选词工具及文章智能标签功能于一体的WordPress网站SEO优化插件。 智能推荐&#xff1a; 热门关键词推荐引擎-支持360搜索、Bing、谷歌&a…

【快刊录用】Springer旗下2区,1个21天录用,12天见刊!

网络安全类SCIE ☑️期刊简介&#xff1a;IF&#xff1a;4.0-5.0&#xff0c;JCR2区&#xff0c;中科院3区 ☑️检索情况&#xff1a;SCIE 在检&#xff0c;正刊 ☑️征稿领域&#xff1a;提高安全性和隐私性的边缘/云的智能方法的研究&#xff0c;如数字孪生等 ☑️录用案…

如何使用支付宝沙箱环境支付并公网调用sdk创建支付单服务

文章目录 1.测试环境2.本地配置2. 内网穿透2.1 下载安装cpolar内网穿透2.2 创建隧道3. 测试公网访问4. 配置固定二级子域名4.1 保留一个二级子域名4.2 配置二级子域名5. 使用固定二级子域名进行访问 1.测试环境 MavenSpring bootJdk 1.8 2.本地配置 获取支付宝支付Java SDK,…

Object.hashCode() 详解

在Java编程中&#xff0c;hashCode方法是一个常见而重要的概念。它通常用于哈希表、集合以及一些需要高效检索数据的数据结构中。然而&#xff0c;对于许多开发者来说&#xff0c;hashCode方法可能是一个容易被忽略或者被简单实现的部分。在本文中&#xff0c;我们将深入探讨Ja…

Azure Machine Learning - Azure OpenAI GPT 3.5 Turbo 微调教程

本教程将引导你在Azure平台完成对 gpt-35-turbo-0613 模型的微调。 关注TechLead&#xff0c;分享AI全维度知识。作者拥有10年互联网服务架构、AI产品研发经验、团队管理经验&#xff0c;同济本复旦硕&#xff0c;复旦机器人智能实验室成员&#xff0c;阿里云认证的资深架构师&…

FreeRTOS的学习

1.创建函数和删除 动态创建为FreeRTOS分配的堆栈&#xff08;方便&#xff09;&#xff0c;而静态创建为人为分配空间。任务中必须有while&#xff08;1&#xff09;否则只会执行一次任务中的延时要用 vTaskDelay(500); 延时期间执行其它任务 任务中的延时使用的是软件…

Qt Creator可视化交互界面exe快速入门2

上一期介绍的通过代码的方式实现一个简单界面&#xff0c;需要敲小几十行代码&#xff0c;显然是效率低的&#xff0c;这期就介绍下Qt Creator的作用。 Qt Creator的使用&#xff1a; 首先打开我们的Qt Creator 然后点击创建项目&#xff0c;在项目Application里面选择Qt Wid…

网站在线客服系统推荐:提升客户体验与满意度

精致的前端页面可以提供访客的用户体验感&#xff0c;连接响应迅速可以帮助客服人员及时联系到访客帮助其解决问题&#xff0c;访客来源明细可以帮助客服人员更加清楚的了解用户需求以展开更有针对性的营销&#xff0c;操作方便有助于公司迅速培训员工&#xff0c;同时也方便客…

Appium如何实现移动端UI自动化测试呢?

Appium是一个开源跨平台移动应用自动化测试框架。 既然只是想学习下Appium如何入门&#xff0c;那么我们就直奔主题。文章结构如下&#xff1a; 为什么要使用Appium&#xff1f;如何搭建Appium工具环境?(超详细&#xff09;通过demo演示Appium的使用Appium如何实现移动端UI自…

一文速览字节最新分布式操作系统KubeWharf

一文速览字节最新分布式操作系统KubeWharf KubeWharf 是字节跳动基础架构团队在对 Kubernetes 进行了大规模应用和不断优化增强之后的技术结晶。 这是一套以 Kubernetes 为基础构建的分布式操作系统&#xff0c;由一组云原生组件构成&#xff0c;专注于提高系统的可扩展性、功…

绝地反击,不做背锅侠!

那么作为运维人员&#xff0c;如何摆脱以上背黑锅的尴尬局面呢&#xff1f;堡垒机当然是破解此局面的绝杀大招。 1.统一登录入口 提供统一入口&#xff0c;集中管理和分配账户密码、所有运维人员只能登录堡垒机才能访问服务器&#xff0c;梳理“人与服务器”之间的关系&#…

案例169:基于微信小程序的小区疫情防控系统

文末获取源码 开发语言&#xff1a;Java 框架&#xff1a;SSM JDK版本&#xff1a;JDK1.8 数据库&#xff1a;mysql 5.7 开发软件&#xff1a;eclipse/myeclipse/idea Maven包&#xff1a;Maven3.5.4 小程序框架&#xff1a;uniapp 小程序开发软件&#xff1a;HBuilder X 小程序…

各种不同的安科瑞电力物联网仪表实现互联互通

安科瑞 崔丽洁 电力物联网是物联网在智能电网中的应用&#xff0c;是有效整合通信基础设施资源和电力基础设施资源&#xff0c;提高电力系统信息化水平&#xff0c;改善电力系统现有基础设施利用效率的重要举措。 电力物联网仪表为终端感知设备&#xff0c;该系列产品将我们多年…