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

I'm working on a v8 module that adds a field 'customer_group' to partners. 

When I change this field on a Company, I want all Contacts connected to this company to also get the same value in their 'customer_group' field. I cannot for the life of me manage to change the 'customer_group' field on the childs of a partner. I am able to change other fields (as a test) on the childs, but not this specific field.

What's wrong with my code?

from openerp import models, fields, api

class CustomerGroup(models.Model):

    _name = 'dummy.customergroup'

    

    name = fields.Char('Group name', required=True)

    description = fields.Text()

class ResPartner(models.Model):

    _inherit = 'res.partner'

    

    customer_group = fields.Many2one('dummy.customergroup', 'Customer group')

    @api.onchange('customer_group')

    def _update_contact_customer_group(self):

        for id in self.child_ids:

            id.street = "TESTSTREET" # AS A TEST, THIS WORKS

            id.title = 1 # AS A TEST, THIS WORKS
# NONE OF THE BELOW WORKS, I'M TRYING AS A TEST TO SET GROUP NUMBER 3 (I HAVE 5 GROUPS CREATED)

id.customer_group = 3

            id.customer_group = self.env['dummy.customergroup'].browse([3])

            id.customer_group = self.env['dummy.customergroup'].browse(3)

Avatar
Discard
Best Answer

Hello Frank,

For that you can make related field to company.

Ex:-

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

    customer_group = fields.Many2one('dummy.customergroup', 'Customer group')

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

    customer_group = fields.Many2one('dummy.customergroup', 'Customer group', related='company_id.customer_group', store=True)


Now, when you change "customer_group" field in company, all the contact's "customer_group" automatically updated.


Hope it will works for you.
Thanks,

Avatar
Discard

makes sense.