Odoo 17中为特定组设置销售订单pricelist_id字段只读
解决Odoo 17销售订单
pricelist_id字段对指定组只读的问题 你之前的写法无效,是因为误用了groups属性——该属性用于控制字段可见性(哪些组能看到字段),而非只读状态。以下是两种可行的解决方案:
方案一:视图层面通过attrs控制(推荐)
在继承销售订单表单视图的XML文件中,修改pricelist_id字段的只读条件:
<record id="sale_order_form_inherit" model="ir.ui.view"> <field name="name">sale.order.form.inherit</field> <field name="model">sale.order</field> <field name="inherit_id" ref="sale.view_order_form"/> <field name="arch" type="xml"> <field name="pricelist_id" position="attributes"> <attribute name="attrs">{'readonly': [('user_has_group', '=', 'new_module.new_group')]}</attribute> </field> </field> </record>
- 效果:所有属于
new_module.new_group的用户,无论是否拥有product.group_product_pricelist权限,均无法编辑该字段。 - 若需仅当用户属于
new_group且不属于product.group_product_pricelist时才只读,可将条件改为:{'readonly': [('user_has_group', '=', 'new_module.new_group'), ('user_has_group', '!=', 'product.group_product_pricelist')]}
方案二:模型层面控制
重写销售订单模型的pricelist_id字段,通过lambda函数动态判断只读状态:
from odoo import models, fields class SaleOrder(models.Model): _inherit = 'sale.order' pricelist_id = fields.Many2one( 'product.pricelist', readonly=lambda self: self.env.user.has_group('new_module.new_group'), groups='product.group_product_pricelist' )
- 保留原
groups属性确保product.group_product_pricelist组用户能看到字段; readonly参数通过lambda判断当前用户是否属于目标组,动态设置只读状态。
注意事项
- 确保
new_module.new_group组的XML定义正确,且模块已安装; - 修改后需升级模块并刷新视图,才能生效;
- 若用户同时属于
new_group和product.group_product_pricelist,两种方案都会优先触发只读规则,符合需求。
内容的提问来源于stack exchange,提问作者Megha
相关产品推荐
相关产品推荐

