Get current logged in user id in odoo

Today I am going to show you 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 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 controller

Inside a 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')