Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
1235 Lượt xem

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 ?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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


Ảnh đại diện
Huỷ bỏ
Tác giả

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

Câu trả lời hay nhất

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




Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 11 24
932
2
thg 6 20
11229
8
thg 4 23
71595
2
thg 1 22
48345
1
thg 12 15
4136