Odoo添加自定义字段后用户档案出现Access Error问题求助
Odoo用户档案Access Error问题分析与解决方案
原因分析
- hr_fleet模块为
res.users模型添加了employee_cars_count计算字段,并将该字段的读取权限限制为Fleet / Administrator组。 - 当你继承基础用户视图(
base.view_users_form_simple_modif)添加自定义字段时,Odoo会自动合并所有继承自该基础视图的子视图,包括hr_fleet模块的视图逻辑。此时非车队管理员用户打开档案时,视图会触发employee_cars_count字段的权限检查,因无权限直接抛出Access Error。 - 即使你的自定义视图未显式引用该字段,合并后的视图仍会包含其权限约束规则,导致权限检查触发。
解决方案
方法1:调整employee_cars_count字段权限
继承res.users模型,重写employee_cars_count字段,将其权限扩展给普通用户组:
from odoo import fields, models class ResUsers(models.Model): _inherit = 'res.users' # 继承原字段并修改权限组,允许所有普通用户读取 employee_cars_count = fields.Integer( compute='_compute_employee_cars_count', groups='base.group_user' ) # 确保计算逻辑与原模块一致,直接继承原方法即可 def _compute_employee_cars_count(self): super()._compute_employee_cars_count()
方法2:在视图中隐藏employee_cars_count字段
通过视图继承将该字段设为不可见,避免触发权限检查:
<?xml version="1.0" encoding="utf-8"?> <odoo> <data> <record id="res_users_view_form_outlook123" model="ir.ui.view"> <field name="name">res.users.preferences.form.inherit123</field> <field name="model">res.users</field> <field name="inherit_id" ref="base.view_users_form_simple_modif"/> <field name="arch" type="xml"> <xpath expr="//field[@name='signature']" position="after"> <field name="microsoft_outlook_uri_incoming"/> </xpath> <!-- 隐藏employee_cars_count字段,跳过权限检查 --> <xpath expr="//field[@name='employee_cars_count']" position="attributes"> <attribute name="invisible">1</attribute> </xpath> </field> </record> </data> </odoo>
方法3:分配独立权限组(无需改代码)
创建新权限组,给该组添加res.users模型中employee_cars_count字段的读取权限,再将需要查看自身档案的用户加入此组。适合不想改动核心代码的场景。
内容的提问来源于stack exchange,提问作者Nir Vana
相关产品推荐
相关产品推荐

