+5 votes

Hi all,
how to get active_id or current user login id?

in Back-end by (3.7k points) | 1.8k views

4 Answers

+5 votes
Best answer

You can get the active user from the environment variable if you are in a Model self.env.user or in the request if you are in a Controller request.env.user
by (650 points)
selected by
+4 votes

there's another context in which you can use the current logged in user ID other than python code

in ir.rule definition you have an attribute which represent the current logged in user as an object

and you can use it this way 


<record id="rule_pos_config_user_allowed" model="ir.rule">

<field name="name">POS Own config</field>

<field name="model_id" ref="point_of_sale.model_pos_config" />

<field name="groups" eval="[(4, ref('point_of_sale.group_pos_user')),(4, ref('point_of_sale.group_pos_manager'))]" />

<field name="domain_force">['|',('pos_users_allowed','in',[user.id]),('create_uid', '=', user.id)]</field>

<field name="perm_read" eval="True" />

<field name="perm_write" eval="True" />

<field name="perm_create" eval="True" />

<field name="perm_unlink" eval="True" />


or in any other xml object 
you can use the current logged in user id as an Integer in domains or attrs ...

attrs="{'invisible':[('user_id', '!=', uid)]}"
<field name="domain">[('partner_id.user_id', '=', uid)]</field>
by (1k points)
+3 votes

You can define a field like this;

current_user = fields.Many2one('res.users','Current User', default=lambda self: self.env.user)

by (8.8k points)
0 votes
active my id account
204 questions
180 answers
265,587 users