How to view or hide field by attrs based on function (Automatically)?

后端 未结 2 711
醉酒成梦
醉酒成梦 2021-01-24 12:05

I want to view \'working_hours\' field only for employee, his manager and \'hr.group_hr_user\' group. how to hide this field automatically without edit form or trigger a

相关标签:
2条回答
  • 2021-01-24 12:33

    i added the field before the function and it works now automatically

    class InheritHrEmployee(models.Model):
        _inherit = 'hr.employee'
    
        inv = fields.Boolean(string="Invisible", compute="c_inv", store=False)
    
        @api.one
        def c_inv(self):
            if self.env.uid == self.user_id.id or self.env.uid == self.parent_id.user_id.id or self.env.user.has_group(
                 'hr.group_hr_user'):
                self.inv = False
            else:
                self.inv = True
    

    .. like this example make fields visible to user and invisible to other

    0 讨论(0)
  • 2021-01-24 12:40

    Try below code for display working hours field only hr.group_hr_user group users.

    XML:

    <record id="hide_working_hours_for_employees" model="ir.ui.view">
      <field name="name">Hide Working Hours Employees Form</field>
      <field name="model">hr.employee</field>
      <field name="inherit_id" ref="hr.view_employee_form"/>
      <field name="arch" type="xml">
        <xpath expr="//field[@name='resource_calendar_id']" position="before">
          <field name="working_hours_view" invisible="1"/>
        </xpath>
        <xpath expr="//field[@name='resource_calendar_id']" position="attributes">
          <attribute name="groups">hr.group_hr_user</attribute>
        </xpath>
      </field>
    </record>
    

    You can add multiple attributes in the XML file like above code.

    0 讨论(0)
提交回复
热议问题