如何为Char字段传递值Many2one? (Odoo 11)

时间:2019-05-01 08:12:48

标签: python xml odoo

如何将many2one字段的值传递给字符字段(Many2one ---> Char),我不知道该怎么做(预先感谢您),示例:

字段Many2one和Char:

class ResCompany(models.Model):
    _inherit = 'res.company'

    manager_company = fields.Many2one('hr.employee')

class HrMission(models.Model):
    _name = 'hr.mission'

    manager_mission = fields.Char(string='manager', default=lambda self: self.default_manager())

    @api.model
    def default_manager(self):
       manager = self.env['res.company'].search([('manager_company','=', True).name
       return manager

1 个答案:

答案 0 :(得分:1)

我知道执行此操作的两种方法:

# first using lambda 
# use lambda when a one line is enough to do the job like in your case 
# you can access the company record directly from the user pass in self 
# self.env.user
manager_mission = fields.Char(string='manager', default=lambda self: self.env.user.company_id.manager_company.name)




# when you need a lot of work this means that you need to define a method for this

# always define the method before the field like in all odoo apps
@api.model
def default_manager(self):
   manager = self.env.user.company_id.manager_company.name
   return managere


# Then define the field and directly affect the method to default
manager_mission = fields.Char(string='manager', default=default_manager)