当前位置: 首页 > news >正文

odoo17核心概念view5——ir_ui_view.py

这是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.modeldef get_views(self, views, options=None):""" Returns the fields_views of given views, along with the fields ofthe 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 (onpython fields) will be evaluated by the client with all the contextvalues 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 globalfilters or the model:return: dictionary with fields_views, fields and optionally filters"""

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

 @api.modeldef _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 providedif not view_id:# <view_type>_view_ref in context can be used to override the default viewview_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.viewview_id = View.default_view(self._name, view_type)if view_id:# read the view with inherited views appliedview = View.browse(view_id)arch = view._get_combined_arch()else:# fallback on default views methods if no ir.ui.view could be foundview = 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.modeldef _get_default_form_view(self):""" Generates a default single-line form view using all fieldsof 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:continueelif field.type in ('one2many', 'many2many', 'text', 'html'):# append to sheet left and right group if neededif 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)

相关文章:

odoo17核心概念view5——ir_ui_view.py

这是view系列的第5篇文章&#xff0c;介绍一下view对应的后端文件ir_ui_view.py&#xff0c;它是base模块下的一个文件 位置&#xff1a;odoo\addons\base\models\ir_ui_view.py 该文件一共定义了三个模型 1.1 ir.ui.view.custom 查询数据库这个表是空的&#xff0c;从名字看…...

截断整型提升算数转换

文章目录 &#x1f680;前言&#x1f680;截断&#x1f680;整型提升✈️整型提升是怎样的 &#x1f680;算术转换 &#x1f680;前言 大家好啊&#xff01;这里阿辉补一下前面操作符遗漏的地方——截断、整型提升和算数转换 看这一篇要先会前面阿辉讲的数据的存储否则可能看不…...

阿里云 ECS Docker、Docker Compose安装

https://help.aliyun.com/document_detail/51853.html https://docs.docker.com/compose/install/ Centos https://blog.csdn.net/Alen_xiaoxin/article/details/104850553 systemctl enable dockerdocker-compose安装 https://blog.csdn.net/qq465084127/article/details/…...

LeetCode——1276. 不浪费原料的汉堡制作方案

通过万岁&#xff01;&#xff01;&#xff01; 题目&#xff0c;给你两个数tomatoSlices和cheeseSlices&#xff0c;然后每制作一个巨无霸汉堡则消耗4个tomatoSlices和1和cheeseSlices&#xff0c;每制作一个小皇堡则需要消耗2个tomatoSlices和1和cheeseSlices。问给你这两个…...

隆道吴树贵:生成式人工智能在招标采购中的应用

12月22日&#xff0c;由中国招标投标协会主办的招标采购数字发展大会在北京召开&#xff0c;北京隆道网络科技有限公司总裁吴树贵受邀出席大会&#xff0c;并在“招标采购数字化交易创新成果”专题会议上发言&#xff0c;探讨生成式人工智能如何在招标采购业务中落地应用。 本次…...

docker搭建kali及安装oneforall

前期docker的安装这里就不用多说了&#xff0c;直接看后面的代码 安装oneforall 1.安装git和pip3 sudo apt update sudo apt install git python3-pip -y2.克隆项目 git clone https://gitee.com/shmilylty/OneForAll.git3.安装相关依赖 cd OneForAll/ sudo apt install pyt…...

【MySQL】数据库之事务

目录 一、什么是事务 二、事务的ACID是什么&#xff1f; 三、有哪些典型的不一致性问题&#xff1f; 第一种&#xff1a;脏读 第二种&#xff1a;不可重复读 第三种&#xff1a;幻读 第四种&#xff1a;丢失更新 四、隔离级别有哪些&#xff1f; &#xff08;1&#xf…...

AGV|RGV小车RFID传感器CNS-RFID-01/1S的RS232通讯联机方法

CNS-RFID-01/1S广泛应用于AGV小车&#xff0c;搬运机器人&#xff0c;无人叉车等领域&#xff0c;用于定位&#xff0c;驻车等应用&#xff0c;可通过多种通讯方式进行读写操作&#xff0c;支持上位机控制&#xff0c;支持伺服电机&#xff0c;PLC等控制设备联机&#xff0c;本…...

【Python可视化系列】一文教会你绘制美观的热力图(理论+源码)

一、问题 前文相关回顾&#xff1a; 【Python可视化系列】一文彻底教会你绘制美观的折线图&#xff08;理论源码&#xff09; 【Python可视化系列】一文教会你绘制美观的柱状图&#xff08;理论源码&#xff09; 【Python可视化系列】一文教会你绘制美观的直方图&#xff08;理…...

百度Apollo五步入门自动驾驶:Dreamview与离线数据包分析(文末赠送apollo周边)

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏:《linux深造日志》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! ⛳️ 粉丝福利活动 ✅参与方式&#xff1a;通过连接报名观看课程&#xff0c;即可免费获取精美周边 ⛳️活动链接&#xf…...

为什么IPv6 可以作为低功耗蓝牙的物联网体系结构?

蓝牙40规范引人了低功耗蓝牙(Bluetooth Low Energy,BLE)技术。低牙是一种低能低延成本的无线通信技术。 与传统蓝牙相比&#xff0c;低功耗蓝牙同样使用24GHz频段,但其将信道重新划分为 40个&#xff0c;包含37 个数据信道和3个广播信道(传统蓝牙共使用 79 个信道)低功蓝牙的协…...

GPT每预测一个token就要调用一次模型

问题&#xff1a;下图调用了多少次模型&#xff1f; 不久以前我以为是调用一次 通过看代码是输出多少个token就调用多少次&#xff0c;如图所示&#xff1a; 我理解为分类模型 预测下一个token可以理解为分类模型&#xff0c;类别是vocab的所有token&#xff0c;每一次调用都…...

运维工程师的出路到底在哪里?

1.35岁被称为运维半衰期&#xff0c;主要是因为运维工作的技术栈和工作方式在不断更新和演进。随着新技术的出现和发展&#xff0c;老旧的技术逐渐被淘汰&#xff0c;运维工作也需要不断学习和适应新技术&#xff0c;否则就容易被市场淘汰。 2.要顺利过渡半衰期&#xff0c;运…...

2312clang,基于访问者的前端动作

原文 基于RecursiveASTVisitor的ASTFrontendActions. 创建用RecursiveASTVisitor查找特定名字的CXXRecordDeclAST节点的FrontendAction. 创建FrontendAction 编写基于clang的工具(如Clang插件或基于LibTooling的独立工具)时,常见入口是允许在编译过程中执行用户特定操作的F…...

怎么搭建实时渲染云传输服务器

实时渲染云传输技术方案&#xff0c;在数字孪生、虚拟仿真领域使用越来越多&#xff0c;可能很多想使用该技术方案项目还不知道具体该怎么搭建云传输服务器&#xff0c;具体怎么使用实时云渲染平台系统。点量云小芹将对这两个问题做集中分享。 一、实时渲染服务器怎么搭建&…...

如何在生产环境正确使用Redis

一、在生产环境使用Redis 如果在生产环境使用Redis&#xff0c;需要遵守一定的使用规范&#xff0c;以保障服务稳定、高效。。 1.1、明确Redis集群的服务定位 1、仅适用于缓存场景&#xff1a;Redis定位于高性能缓存服务&#xff0c;强调快速读写和低延迟的特性&#xff0c;…...

LeetCode-环形链表问题

1.环形链表&#xff08;141&#xff09; 题目描述&#xff1a; 给你一个链表的头节点 head &#xff0c;判断链表中是否有环。 如果链表中有某个节点&#xff0c;可以通过连续跟踪 next 指针再次到达&#xff0c;则链表中存在环。 为了表示给定链表中的环&#xff0c;评测系统…...

C# 读取Word表格到DataSet

目录 功能需求 Office 数据源的一些映射关系 范例运行环境 配置Office DCOM 关键代码 组件库引入 ​核心代码 杀掉进程 总结 功能需求 在应用项目里&#xff0c;多数情况下我们会遇到导入 Excel 文件数据到数据库的功能需求&#xff0c;但某些情况下&#xff0c;也存…...

构建外卖系统:从技术到实战

在当今高度数字化的社会中&#xff0c;外卖系统的开发变得愈发重要。本文将从技术角度出发&#xff0c;带领读者一步步构建一个基础的外卖系统&#xff0c;并涵盖关键技术和实际代码。 1. 技术选型 1.1 后端开发 选择Node.js和Express框架进行后端开发&#xff0c;搭建一个灵…...

城市之眼:数据可视化在智慧城市的角色

作为智慧城市建设的核心组成部分&#xff0c;数据可视化扮演着至关重要的角色。在城市中&#xff0c;数据源源不断地产生&#xff0c;涵盖了从交通流量、环境质量到市民需求等各个方面。而数据可视化作为将这些数据呈现出来的手段&#xff0c;对智慧城市的发展起着关键性的作用…...

云计算——弹性云计算器(ECS)

弹性云服务器&#xff1a;ECS 概述 云计算重构了ICT系统&#xff0c;云计算平台厂商推出使得厂家能够主要关注应用管理而非平台管理的云平台&#xff0c;包含如下主要概念。 ECS&#xff08;Elastic Cloud Server&#xff09;&#xff1a;即弹性云服务器&#xff0c;是云计算…...

电脑插入多块移动硬盘后经常出现卡顿和蓝屏

当电脑在插入多块移动硬盘后频繁出现卡顿和蓝屏问题时&#xff0c;可能涉及硬件资源冲突、驱动兼容性、供电不足或系统设置等多方面原因。以下是逐步排查和解决方案&#xff1a; 1. 检查电源供电问题 问题原因&#xff1a;多块移动硬盘同时运行可能导致USB接口供电不足&#x…...

解决本地部署 SmolVLM2 大语言模型运行 flash-attn 报错

出现的问题 安装 flash-attn 会一直卡在 build 那一步或者运行报错 解决办法 是因为你安装的 flash-attn 版本没有对应上&#xff0c;所以报错&#xff0c;到 https://github.com/Dao-AILab/flash-attention/releases 下载对应版本&#xff0c;cu、torch、cp 的版本一定要对…...

HTML前端开发:JavaScript 常用事件详解

作为前端开发的核心&#xff0c;JavaScript 事件是用户与网页交互的基础。以下是常见事件的详细说明和用法示例&#xff1a; 1. onclick - 点击事件 当元素被单击时触发&#xff08;左键点击&#xff09; button.onclick function() {alert("按钮被点击了&#xff01;&…...

自然语言处理——Transformer

自然语言处理——Transformer 自注意力机制多头注意力机制Transformer 虽然循环神经网络可以对具有序列特性的数据非常有效&#xff0c;它能挖掘数据中的时序信息以及语义信息&#xff0c;但是它有一个很大的缺陷——很难并行化。 我们可以考虑用CNN来替代RNN&#xff0c;但是…...

【JavaSE】绘图与事件入门学习笔记

-Java绘图坐标体系 坐标体系-介绍 坐标原点位于左上角&#xff0c;以像素为单位。 在Java坐标系中,第一个是x坐标,表示当前位置为水平方向&#xff0c;距离坐标原点x个像素;第二个是y坐标&#xff0c;表示当前位置为垂直方向&#xff0c;距离坐标原点y个像素。 坐标体系-像素 …...

Redis数据倾斜问题解决

Redis 数据倾斜问题解析与解决方案 什么是 Redis 数据倾斜 Redis 数据倾斜指的是在 Redis 集群中&#xff0c;部分节点存储的数据量或访问量远高于其他节点&#xff0c;导致这些节点负载过高&#xff0c;影响整体性能。 数据倾斜的主要表现 部分节点内存使用率远高于其他节…...

USB Over IP专用硬件的5个特点

USB over IP技术通过将USB协议数据封装在标准TCP/IP网络数据包中&#xff0c;从根本上改变了USB连接。这允许客户端通过局域网或广域网远程访问和控制物理连接到服务器的USB设备&#xff08;如专用硬件设备&#xff09;&#xff0c;从而消除了直接物理连接的需要。USB over IP的…...

Hive 存储格式深度解析:从 TextFile 到 ORC,如何选对数据存储方案?

在大数据处理领域&#xff0c;Hive 作为 Hadoop 生态中重要的数据仓库工具&#xff0c;其存储格式的选择直接影响数据存储成本、查询效率和计算资源消耗。面对 TextFile、SequenceFile、Parquet、RCFile、ORC 等多种存储格式&#xff0c;很多开发者常常陷入选择困境。本文将从底…...

论文阅读笔记——Muffin: Testing Deep Learning Libraries via Neural Architecture Fuzzing

Muffin 论文 现有方法 CRADLE 和 LEMON&#xff0c;依赖模型推理阶段输出进行差分测试&#xff0c;但在训练阶段是不可行的&#xff0c;因为训练阶段直到最后才有固定输出&#xff0c;中间过程是不断变化的。API 库覆盖低&#xff0c;因为各个 API 都是在各种具体场景下使用。…...