Search Here

Get Current Logged in Active User ID in Odoo

In this article you will learn how to get active_id or current user login id. The question is why we need this id. The answer is that on the basis of current user we want to perform some action or show records/data on list or tree view, on odoo field or on odoo controller. In this tutorial I will show you different ways to get current user or logged in user id or active id.



Odoo get current logged in user id or active id

Get current logged In user id in Odoo/openERP model

Get current logged in user id in field:


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

We can also get current user id or logged in user id using "self.env.uid"

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

We can also get current user id by defining default value of a field. Default value will be a function that returns the current user's id which can be accessed with self.env.uid. By using default function we can set any value to a field.

def get_user_id(self):
return self.env.uid
current_user = fields.Many2one('res.users','Current User', default=_get_user_id)

Get current user or logged in user id in odoo web controller

Inside a custom or inherited web controller we can get the current user id by following codes.

request.uid 
or
request.session.uid 
or
http.request.env.context.get('uid') 

Post a Comment

6 Comments

  1. We have a lot of ways to get the current user, so what is the best way? and why?

    ReplyDelete
    Replies
    1. there is no time complexity involve to get the current user, so u can use any one of them.

      Delete
  2. amazing blog for odoo development please make tutorial with NEW API

    ReplyDelete
    Replies
    1. thanks for appreciation, we will do our best to save dev lives.

      Delete
  3. Hello, thank you for sharing, anw how to put it on email template ya?

    ReplyDelete