In this tutorial, i will teach you the simplest way to get the currently logged in user in Odoo. These tutorial is based on version 8, 9 and 10.
Let's get to it...
To get the currently logged user in Odoo, i will create a small method that gets the logged in user and default the method on a field.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _employee_get(self): | |
record = self.env['res.users'].search([('name', '=', self.env.user.name)]) | |
return record.name |
Now that you have the method, all you need is to default the method on a field. The field you are returning the method on, can either be Char or a Many2one field, it's your choice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
logged_user = fields.Char(string="Logged User", default=_employee_get) |
If your field is Many2one, then you dont need to return record.name, all you need is just to return record alone, and the Many2one field will do the magic.
Now, let's put the pieces together...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from odoo import fields, models, api | |
class RecruitmentForm(models.Model): | |
_name = 'recruitment.application' | |
# method to return logged in user on a Char field | |
def _employee_get(self): | |
record = self.env['res.users'].search([('name', '=', self.env.user.name)]) | |
return record.name | |
logged_user = fields.Char(string="Logged User", default=_employee_get) # Char field | |
# method to return logged in user on a Many2one field | |
def _employee_get(self): | |
record = self.env['res.users'].search([('name', '=', self.env.user.name)]) | |
return record | |
logged_user = fields.Many2one('res.users', string="Logged User", default=_employee_get) # Many2one |
No comments:
Post a Comment