Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
3 Replies
1094 Tampilan

Hi everyone,

I'm working on an Odoo 17 Community Edition project.

In the Employee form view (hr.employee.form), I want to hide the "Private Information" tab unless:

  • The logged-in user is viewing their own employee record, or
  • The logged-in user is an administrator.

Here's what the view looks like in XML:


<page name="personal_information" string="Private Information">

    <!-- Some private fields here -->

</page>



how can i fix it ?

Avatar
Buang
Jawaban Terbai

Hi,


You can hide the page  'Private Information' based on the condition.

Try the following.

XML

<record id="view_employee_form" model="ir.ui.view">
<field name="name">hr.employee.inherit.employee</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form"/>
<field name="arch" type="xml">
<xpath expr="//page[@name='personal_information']" position="attributes">
<attribute name="invisible">user_id != uid or uid.is_admin == False</attribute>
</xpath>
</field>
</record>

From UI

* Enable developer mode.

Edit the form view.

In these ways you can hide the page.


Hope it helps


Avatar
Buang
Penulis

YOU ARE THE BEST !!!!!!!!!!!!!!

Jawaban Terbai

Hello,
Unfortunately, in Odoo 17 Community Edition, you cannot fully implement that logic (check if user is admin or viewing their own employee record) using XML alone.

Why Not?
  • XML views (attrs, groups, etc.) can only react to field values, not to dynamic logic like comparing current user to a record's user_id.
  • There’s no built-in XML expression like “if current user is this employee.”

For Example:
XML views don’t allow direct evaluation of expressions like: id == user_id.employee_id.id
because:

  • XML attrs only checks field values in the current record.
  • It doesn't know who the current user is unless you expose that data to the view (via a computed field).

Correct Way Using Minimal Python (with Your Insight!)



Step 1: Minimal Python Code

from odoo import models, fields


class Employee(models.Model):

    _inherit = 'hr.employee'


    is_self_or_admin = fields.Boolean(compute='_compute_is_self_or_admin')


    def _compute_is_self_or_admin(self):

        user = self.env.user

        for record in self:

            record.is_self_or_admin = (

                record.user_id.id == user.id or user.has_group('base.group_system')

            )
Step 2: Use it in XML
<odoo>

    <record id="view_employee_form_inherit_hide_private_info" model="ir.ui.view">

        <field name="name">hr.employee.form.hide.private.info</field>

        <field name="model">hr.employee</field>

        <field name="inherit_id" ref="hr.view_employee_form"/>

        <field name="arch" type="xml">


            <!-- Use xpath to locate the page by name -->

            <xpath expr="//page[@name='personal_information']" position="attributes">

                <attribute name="attrs">{'invisible': [('is_self_or_admin', '=', False)]}</attribute>

            </xpath>


        </field>

    </record>

</odoo>

Now the tab only shows if:

  • The logged-in user is the employee linked to the record, or
  • The user is an Admin


Thanks & Regards,
Saiyed Mahedi Abbas
Odoo Developer
Email:- saiyedmehdiabbas8@gmail.com




Avatar
Buang
Post Terkait Replies Tampilan Aktivitas
0
Nov 24
891
2
Jun 20
11164
8
Apr 23
71384
2
Jan 22
48143
1
Des 15
4054