当前位置:网站首页>odoo多公司
odoo多公司
2022-07-13 19:18:00 【姜振建 15954039008】
从 odoo 13版本开始,一个用户可以同时登录多个公司。这允许用户访问来自多个公司的信息,还可以在多公司环境中创建/编辑记录。
如果处理不当,会出现一些问题。例如,可以同时登录公司 A 和 B 的用户可以在公司 A 中创建销售订单并将属于公司 B 的产品添加到其中。只有当用户从 B 公司注销时,销售订单才会出现访问错误。
那么在多公司的操作上,odoo是如何处理细节的呢?
1、公司相关字段
当一条记录可从多家公司获得时,我们必须预期将根据设置值的公司为给定字段分配不同的值。
要使同一记录的字段支持多个值,必须将其定义为属性 company_dependent设置为True。
from odoo import api, fields, models
class Record(models.Model):
_name = 'record.public'
info = fields.Text()
company_info = fields.Text(company_dependent=True)
display_info = fields.Text(string='Infos', compute='_compute_display_info')
@api.depends_context('company')
def _compute_display_info(self):
for record in self:
record.display_info = record.info + record.company_info
注:depends_context后的参数是上下文context中的key值
该_compute_display_info方法用depends_context(‘company’) 修饰,以确保根据当前公司 ( self.env.company) 重新计算计算域,如为uid则是当前用户。
*读取公司相关字段时,用当前公司的数据。换句话说,如果用户以 A 为主要公司登录 A 公司和 B 公司,并为 B 公司创建记录,则公司相关字段的值将是 A 公司的值。
*要读取从另一家公司而不是当前公司设置的公司相关字段的值,我们需要确保我们使用的公司是正确的。这可以通过with_company()更新当前公司来完成。
# 当前用户所属的公司为主公司 (self.env.company)
val = record.company_dependent_field
# 目前操作的公司为主公司 (company_B)
val = record.with_company(company_B).company_dependent_field
# record.with_company(company_B).env.company == company_B
注:在操作时一定要知道当前交互的公司到底是哪个:
@api.onchange('field_name')
def _onchange_field_name(self):
self = self.with_company(self.company_id)
...
@api.depends('field_2')
def _compute_field_3(self):
for record in self:
record = record.with_company(record.company_id)
...
2、多公司一致性
当一条记录通过一个字段在几家公司之间共享company_id时,我们须注意它不能通过关系字段链接到另一家公司的记录。例如,我们不希望销售订单及其发票属于不同的公司。
为确保这种多公司一致性,必须:
*将类属性设置_check_company_auto为True。
*如果模型具有 字段,则使用属性check_company设置为定义关系字段。Truecompany_id
*在每个create()和write()上,都会触发自动检查,以确保记录的多公司一致性。
from odoo import fields, models
class Record(models.Model):
_name = 'record.shareable'
_check_company_auto = True
company_id = fields.Many2one('res.company')
other_record_id = fields.Many2one('other.record', check_company=True)
注:a.该字段company_id不得使用 定义check_company=True。
b.该check_company功能执行严格检查!这意味着如果一条记录没有 company_id(即该字段不是必需的),则它不能链接到 company_id已设置的记录。
c.当字段上没有定义域并check_company设置为 时True,将添加一个默认域:[‘|’, '(‘company_id’, ‘=’, False), (‘company_id’, ‘=’, company_id)]
3、默认公司
当模型需要该字段company_id时,建议的做法是设置默认公司。它简化了用户的设置流程,甚至在公司从视图中隐藏时保证其有效性。事实上,如果用户没有访问多个公司的权限(即当用户没有组时base.group_multi_company),公司通常是隐藏的。
from odoo import api, fields, models
class Record(models.Model):
_name = 'record.restricted'
_check_company_auto = True
company_id = fields.Many2one(
'res.company', required=True, default=lambda self: self.env.company
)
other_record_id = fields.Many2one('other.record', check_company=True)
如上所述,如果用户无权访问多家公司,则该公司通常是隐藏在视图之外的。对应的群组: base.group_multi_company。
4、视图
<record model="ir.ui.view" id="record_form_view">
<field name="name">record.restricted.form</field>
<field name="model">record.restricted</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<group>
<field name="company_id" groups="base.group_multi_company"/>
<field name="other_record_id"/>
</group>
</group>
</sheet>
</form>
</field>
</record>
5、设定安全规则
在处理跨公司共享或仅限于单个公司的记录时,我们必须注意用户无权访问属于其他公司的记录。
这是通过基于 的安全规则实现的company_ids,其中包含用户的当前公司(用户在多公司widget中检查的公司)。
<!-- Shareable Records -->
<record model="ir.rule" id="record_shared_company_rule">
<field name="name">Shared Record: multi-company</field>
<field name="model_id" ref="model_record_shared"/>
<field name="global" eval="True"/>
<field name="domain_force">
['|', ('company_id', '=', False), ('company_id', 'in', company_ids)]
</field>
</record>
<!-- Company-restricted Records -->
<record model="ir.rule" id="record_restricted_company_rule">
<field name="name">Restricted Record: multi-company</field>
<field name="model_id" ref="model_record_restricted"/>
<field name="global" eval="True"/>
<field name="domain_force">
[('company_id', 'in', company_ids)]
</field>
</record>
其他参考文档:
https://blog.csdn.net/u012739578/article/details/121670129?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165552927016781683949249%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=165552927016781683949249&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allbaidu_landing_v2~default-2-121670129-null-null.142v17pc_search_result_control_group,157v15new_3&utm_term=odoo+%E5%A4%9A%E5%85%AC%E5%8F%B8&spm=1018.2226.3001.4187
边栏推荐
- arduino上传程序出错不成功常见的问题解决
- dtcloud的消息机制(三)
- Famous pipeline principle and detailed explanation (very practical)
- Vscode input! No prompt, unable to automatically complete the solution (latest)
- scala快速入门
- input获取焦点
- 【IDEA】check out master invalid path 问题
- 三种方法模拟实现库函数strlen,加深对strlen的理解
- shell语法简单介绍
- Anonymous pipeline principle and detailed explanation (very practical)
猜你喜欢

Virtualization path of GPU resource pool

What happens when you unplug the power? Gaussdb (for redis) dual life keeps you prepared

Hcip second experiment

babylon.js高度图

【IDEA】check out master invalid path 问题

首届人工智能安全大赛启动报名 三大赛题等你来战

音视频学习(六)——PCM音频基础

Web安全-ReDos正则表达式的拒绝服务攻击

How to recover the files deleted by win11 security center?

【虹科技术】网络万用表在数据中心的应用
随机推荐
HCIP第二个实验
Jerry's dot matrix screen displays the Chinese Bluetooth name [article]
人工智能与 RPA 技术应用(一)-RPA弘玑产品介绍、设计器界面功能讲解
为什么越来越多的人要考PMP项目管理认证?
曹洁 Spark编程Scala版本课后习题答案
QT+VS 工程在 Release/Debug 文件夹下导入相关 DLL (非常实用)
Hcip second day notes
Hardware iic+dma operation sharing based on stm32f405
Why do more and more people want to take the PMP project management certification?
【虹科技术】网络万用表在数据中心的应用
How to manage win11 local users and groups? Win11 method of creating user administrator
arduino上传程序出错不成功常见的问题解决
[go] Ⅱ. Introduction à l'API reposante et au processus et à la structure du Code de l'API
No one really thinks that chatting robots are difficult -- using Bert to load the pre training model to get Chinese sentence vectors
LeetCode 1584. 连接所有点的最小费用
【canvas】canvas和svg基本使用
LeetCode 1584. Minimum cost of connecting all points
红柳林,煤矿里挖出了数据金矿!
第三讲:股票买卖 III
Programmer transformation project management? Get a PMP?