Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
3068 Vistas

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 Mejor respuesta

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
Mejor respuesta

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?

Publicaciones relacionadas Respuestas Vistas Actividad
3
mar 24
1448
1
jul 23
2253
2
mar 24
1534
3
dic 23
5926
3
ago 23
2874