Skip to Content
Menu
This question has been flagged
3 Replies
3253 Views

I want to add a group selection button and is_group boolean field to inherited res.partner model but when added the group button the function compute_company_type not working

my code is

class Partner(models.Model):
    _inherit = 'res.partner'

    company_type = fields.Selection(selection_add=[('group', 'Group')])
    refered = fields.Many2one('res.partner',string="Refered By")
    import1 = fields.Float(string="Import")
    temp_import = fields.Float(string="Temporary Import")
    export = fields.Float(string="Export")
    temp_export = fields.Float(string="Temporary Export")
    transit = fields.Float(string="Transit")
    group_id = fields.Many2one('res.group', string='Related Group', index=True)
    is_group = fields.Boolean(string='Is a group', default=False)
    is_company = fields.Boolean(string='Is a company', default=False)
   
    @api.depends('is_company', 'is_group')
    def _compute_company_type(self):
        for partner in self:
            if partner.is_group:
                partner.company_type = 'group'
            elif partner.is_company:
                partner.company_type = 'company'
            else:
                partner.company_type = 'person'
   
    def _write_company_type(self):
        for partner1 in self:
            if partner1.company_type == 'group':
                partner1.is_group = partner1.company_type
            elif partner1.company_type == 'company':
                partner1.is_company = partner1.company_type
            else:
                partner1.company_type = 'person'   

    @api.onchange('company_type')
    def onchange_company_type(self):
        for partner in self:
            if partner.company_type == 'group':
                partner.is_group = (partner.company_type == 'group')
            elif partner.company_type == 'company':
                partner.is_company = (partner.company_type == 'company')
            else:
                partner.company_type = 'person'

Avatar
Discard

Did you check that is_group field is getting True or Not?

Author

Yes, group is getting True

Best Answer

Hi, for the record, I solved it like this my code.

Note that, in my case, when I select "group", I want "is_company" to be "true".

But as if "is_company" is true, company_type is set to "company", I had to update "is_group" first and take it into account in  def _compute_company_type.



# company_type is only an interface field, do not use it in business logic
company_type = fields.Selection(selection_add=[('group', 'Group')])
is_group = fields.Boolean(string='Is a group', default=False)

@api.depends('is_company')
def _compute_company_type(self):
    for partner in self:
        if partner.is_group == True:
            partner.company_type = 'group'
        else:
        partner.company_type = 'company' if partner.is_company else 'person'

def _write_company_type(self):
    for partner in self:
        if partner.company_type == 'group':
            self.is_group = True
            self.is_company = True
        else:
            partner.is_company = partner.company_type == 'company'

@api.onchange('company_type')
def onchange_company_type(self):
    for partner in self:
        if self.company_type == 'group':
            self.is_group = True
            self.is_company = True
         else: 
            self.is_company = (self.company_type == 'company')

Avatar
Discard