Skip to Content
Menu
This question has been flagged

I added a new field to the res.partner model as follow:

beneficiary_ids = fields.Many2many('res.partner', 'res_partner_beneficiaries_rel', column1='col_1_partner', column2='col_2_beneficiary', ondelete='cascade' )

That's because each contact can have many beneficiaries and each beneficiary can be included in many contacts.

So, I can add some beneficiaries to a contact.

In another view in another module at first, I select a contact, and in the next field, I want to choose one of its beneficiaries. So, I need to filter contacts to see just the selected contact's beneficiaries in the previous field.

My questions:

​1) Is the Many2many relation correct(or such relation should be Many2one?why?)? 

​2) And, How can I set the right filter/domain? Now, I see all contacts in the second field.


Avatar
Discard
Author Best Answer

This is the right answer I found at last:

@api.onchange('first_contact')

def_onchange_first_contact(self):

​return {'domain': {'next_contact': [('id', 'in', self.first_contact.beneficiary_ids.ids)]}}

No need to add anything else to the two fields.

Avatar
Discard
Best Answer

You can use onchange to get dynamic domain. 

@api.onchange('contact field')

def onchange_contact_field(self):

return {'beneficiary_ids': {'domain': [('id', 'in', self.contact_field.beneficiary_ids.ids)]}}


Avatar
Discard
Author

ok, thanks. I do that but still, have a problem.
what about 'next field' (see 'next filed' in line #7 of my description above)? Should I set *domain* on this field to call *onchange* function? how exactly?

Related Posts Replies Views Activity
3
Mar 24
1356
1
Jul 23
2147
2
Mar 24
1385
3
Dec 23
5652
3
Aug 23
2735