تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
3 الردود
1204 أدوات العرض

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 ?

الصورة الرمزية
إهمال
أفضل إجابة

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


الصورة الرمزية
إهمال
الكاتب

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

أفضل إجابة

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




الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
0
نوفمبر 24
927
2
يونيو 20
11221
8
أبريل 23
71553
2
يناير 22
48321
1
ديسمبر 15
4125