Skip to Content
Menu
This question has been flagged
1 Reply
4527 Views

Whenever I create a new user, a new res.partner record is automatically generated. So, I find the create function in res.partner class in addons. But I cannot find it. And I inherited the res.partner class as follow.

class ResPartner(models.Model):
_name = 'res.partner'
_inherit = 'res.partner'
_description = 'Res Partner'

department_id = fields.Many2one('hr.department', 'Department')

@api.model
def create(self, vals):
res = super(ResPartner, self).create(vals)
company_id = res.company_id.id
return res
In the dictionary vals, there is no department_id, so I want to add department_id to vals. How can I 
achieve this? Thanks in advance!
Avatar
Discard
Best Answer

Hello,

You can search for related department and add it in vals

class ResPartner(models.Model):
_name = 'res.partner'
_inherit = 'res.partner'
_description = 'Res Partner'

department_id = fields.Many2one('hr.department', 'Department')

@api.model
def create(self, vals):
department = self.env['hr.department'].search([<your search domain>],limit=1)
if department :
vals['department_id'] = department.id
res = super(ResPartner, self).create(vals)
company_id = res.company_id.id
return res
Avatar
Discard
Related Posts Replies Views Activity
2
Feb 23
2765
2
Feb 16
12870
1
Dec 23
18553
0
Jul 24
159
0
Dec 20
3544