Skip to Content
Menú
This question has been flagged
2 Respostes
3156 Vistes

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
Descartar
Autor 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
Descartar
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
Descartar
Autor

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 Respostes Vistes Activitat
3
de març 24
1522
1
de jul. 23
2320
2
de març 24
1602
3
de des. 23
6065
3
d’ag. 23
2964