M.Lamine Lalmi
M.Lamine Lalmi

Reputation: 88

How to get the current (logged) user in XML Odoo V11?

I'm working on Employee Directory module in odoo11 and I want to set a notebook page invisible to the current user (Logged user) if he is different to the related user.

I tried to use user.id in XML but it does not work.

Here is my code :

<page name="hr_settings" string="HR Settings" attrs="{'invisible':[('user_id', '!=', user.id)]}">
    <group>
        <group string='Status' name="active_group">
            <field name="company_id"/>
                <field name="user_id" string="Related User"/>
            </group>
        </group>
    </page>

The error message :

<class 'NameError'>: "name 'user' is not defined" while evaluating

"{'invisible': [('user_id', '!=', user.id)]}"

None" while parsing /opt/odoo/odoo/my_addons/hr_dz/views/employee_views.xml:5, near

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

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

Any idea about that, please ?

Upvotes: 2

Views: 11617

Answers (2)

DASADIYA CHAITANYA
DASADIYA CHAITANYA

Reputation: 2892

In your case basically, we can achieve directly with uid global expression variable on view level.

uid is also used into the expression evaluation in odoo xml view file

<page name="hr_settings" string="HR Settings" attrs="{'invisible':[('user_id', '!=', uid)]}">
    <group>
        <group string='Status' name="active_group">
            <field name="company_id"/>
                <field name="user_id" string="Related User"/>
            </group>
        </group>
    </page>

No need to add and create any compute field for the code.

Please see the following view under the addons of Odoo V11

addons/project/project_view.xml.

Upvotes: 3

Bhoomi Vaishnani
Bhoomi Vaishnani

Reputation: 718

One of the thing i know to use a field in attrs the field must be Mentionsed in the form. i don't know how to get the value of the user id in the form. but if there is not a short way like uid or user you can work arround this, just create a m2o field to res.users make this field compute field with store = False.

Please try this it useful to you.:

# by default store = False this means the value of this field
# is always computed.
current_user = fields.Many2one('res.users', compute='_get_current_user')

@api.depends()
def _get_current_user(self):
    for rec in self:
        rec.current_user = self.env.user
    # i think this work too so you don't have to loop
    self.update({'current_user' : self.env.user.id})

and you can use this field in your form.

<page name="hr_settings" string="HR Settings" attrs="{'invisible':[('user_id', '=', current_user)]}">
    <group>
        <group string='Status' name="active_group">
            <field name="company_id"/>
            <field name="user_id" string="Related User"/>
        </group>
    </group>
</page>

Upvotes: 5

Related Questions