Search code examples
pythonodooodoo-8odoo-9

How to store the current logged user id or active user id in odoo 9?


In expenses, anyone has to approve the expense list, I want to display which user is Approved that?

I just select all res.users as one2many

approved_by=fields.Many2one('res.users','Approved By')

In XML

<field name="approved_by" />

Here it will display all users. But I want to display only logged user and it must be selected as default.


Solution

  • The Environment stores various contextual data used by the ORM. For more details Odoo Environment

    Try with following code.

    approved_by = fields.Many2one('res.users','Approved By', default=lambda self: self.env.user)
    

    EDIT

    With following code, we can also get User id.

    approved_by = fields.Many2one('res.users','Approved By', default=lambda self: self.env.uid)