This question has been flagged
2 Replies
1985 Views

Hi all,

I want to implement a default_adress boolean for child partner of company.

I made a boolean field which I added on the contact form (the windows that appears when you click on a kanban item, at the bottom of the parent partner's form)

I defined a onchange function to watch when this field is edited and then if it is set to True I want to set all others at false.

But it does not work. If I write the value on the record, then they are updated only in the database, and not in the UI.

I think I have to deal with the return value of the on change function but I don't know how to reach others childs of the parent.

Hope someone could help.

Here is my code:

    default_address = fields.Boolean('Default address')
    @api.onchange('default_address')
    def onchange_default_address(self):
        if self.type in ['delivery', 'invoice']:
            if self.default_address is True:
                partners = self.search([('type', '=', self.type),
                                        ('default_address', '=', True)]) - self
                if partners:
                    res = {
                        'value': {
                            'parent_id.child_ids': []
                        }
                    }
                    for partner in partners:
                        res['value']['parent_id.child_ids'] += (1, partner.id,
                                                      {'default_address':
                                                       False})
                    _logger.critical(res)
                    return res
Avatar
Discard
Author Best Answer

Thanks for your response Ahmed.

I don't need to add the domain to my search, because search is done on a set of transient model that are displayed in the kanban view. If I display the partner record obtained, I can see they are those I expected. I did try to directly set using dot notation but it does nothing. However, if I write on the partners the value of default_address, they are updated in the database only, not in the UI. What I want to achieve is to update them on the UI and save when I click on Save.

Also, I tried to return True, but it raises an error

"Type Error argument of type bool is not iterable"

Code:

@api.onchange('default_address')

    def onchange_default_address(self):
        if self.type in ['delivery', 'invoice']:
            if self.default_address is True:
                partners = self.search([('type', '=', self.type),
                                        ('default_address', '=', True)]) - self
                if partners:
                    for partner in partners:
                        partner.default_address = False
                        _logger.critical(partner.id) #The ids are those of partners I want to edit
              
 


Avatar
Discard
Best Answer

Hello,

With the new API you don't need to return a dict with 'value' key. You can simply use the dot [.] to write the value as:

partner.default_address = False ...


I didn't get what you're trying to achieve but I'll try to modify your code, and I believe you'll get the idea ;)


    @api.onchange('default_address')

def onchange_default_address(self):

if self.type in ['delivery', 'invoice']:

if self.default_address is True:

partners = self.search([('type', '=', self.type),

('default_address', '=', True)]) - self

                                         #I think you need to add to the domain parent_id or something ...

if partners:

                    partners.default_address = False

_logger.critical(res)

return True



Hope this could help a little bit ...


Avatar
Discard