代码之家  ›  专栏  ›  技术社区  ›  user1576199

如何根据组和状态使字段只读?

  •  18
  • user1576199  · 技术社区  · 13 年前

    我想做田野 readonly 基于组和状态。就像我有两组:

    1. 经理组
    2. 用户组

    如果我给予 用户组 到 任何用户 然后改变 地位 到 多恩 ,则字段将为 只读的 对于该用户。

    希望我能把它说清楚。谢谢

    4 回复  |  直到 5 年前
        1
  •  33
  •   ChesuCR Ayansplatt    8 年前

    创建布尔类型的函数字段。如果登录的用户在用户组下并且状态已完成,则返回true。然后在视图中,指定 attrs="{'readonly':[('boolean_field_name','=',True)]}"

    或

    首先创建表单视图。然后继承视图并指定组。例如,在销售订单表单视图中,当状态不在草稿或已发送时,我想使客户参考字段仅为组用户只读。

    <record id="view_order_form_cust_ref_readonly" model="ir.ui.view">
        <field name="name">sale.order.form.readonly.cust</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="groups_id" eval="[(6, 0, [ref('base.group_user') ])]"/>
        <field name="arch" type="xml">
            <field name='client_order_ref'" position="attributes">
                <attribute name="attrs">{'readonly':[('state','not in',['draft','sent'])]}</attribute>
            </field>
        </field>
    </record>
    
        2
  •  4
  •   Heroic    13 年前

    您可以在OpenERP中的字段级别应用访问规则,就像在py中一样

    'name': fields.char('Name', size=128, required=True, select=True,
     read=['base.group_user'] ),
    

    对于xml中的状态:

    <field name="name " attrs="{'readonly': [('state','=','done')]}"/>
    
        3
  •  3
  •   Emipro Technologies Pvt. Ltd.    9 年前

    实现这一点还有另一种甜蜜的方式。创建一个功能字段,并在该字段中检查分配给该用户的组,不要存储该字段。在视图中,在属性中使用该字段。

    比如说,在你不想让任何用户修改的产品中 内部参考 如果用户不属于 产品修改 组

    创建一个组。

    <data noupdate="1" >
        <record model="res.groups" id="group_product_modify">
            <field name="name">Product Modify</field>
            <field name="users" eval="[(4, ref('base.user_root'))]"/> 
        </record>
    </data>     
    

    Python文件

    class product_template(models.Model):
        _inherit="product.template"
    
        @api.one
        def set_access_for_product(self):
            self.able_to_modify_product = self.env['res.users'].has_group('product_extended_ecom_ept.group_product_modify')
    
        able_to_modify_product = fields.Boolean(compute=set_access_for_product, string='Is user able to modify product?')
    

    XMl公司 文件应该看起来像,

    <record model="ir.ui.view" id="product_template_update_internal_code_ept">
                <field name="name">Product Template extension</field>
                <field name="inherit_id" ref="product.product_template_only_form_view"/>
                <field name="model">product.template</field>
                <field name="priority" eval="50" />
                <field name="arch" type="xml">
                    <field name="default_code" position="before">
                        <field name="able_to_modify_product" invisible="1" />
                    </field>
                    <field name="default_code" position="attributes">
                        <attribute name="attrs">{'readonly' : [('able_to_modify_product','=',False)]}</attribute>
                    </field>
                </field>
            </record>   
    
        4
  •  0
  •   Sankalp Kataria    9 年前

    如果您使用Odoo web客户端(GUI)而不是代码,那么有一种非正统的方法可以做到这一点。 只需复制字段,该字段将包含与原始字段相同的值(在中给出原始字段名称 Related Field 在下面 Advanced Properties )并将其标记为只读。

    enter image description here 然后,您可以向无法编辑该字段的用户隐藏原始字段,并向可以使用groups属性进行编辑的用户隐藏复制字段。

    enter image description here

    推荐文章