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.
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.uidcurrent_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')
6 Comments
We have a lot of ways to get the current user, so what is the best way? and why?
ReplyDeletethere is no time complexity involve to get the current user, so u can use any one of them.
Deleteamazing blog for odoo development please make tutorial with NEW API
ReplyDeletethanks for appreciation, we will do our best to save dev lives.
DeleteHello, thank you for sharing, anw how to put it on email template ya?
ReplyDeletethankyou sir
ReplyDelete