跳至內容
選單
此問題已被標幟
2 回覆
3453 瀏覽次數

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.


頭像
捨棄
作者 最佳答案

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.

頭像
捨棄
最佳答案

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)]}}


頭像
捨棄
作者

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?

相關帖文 回覆 瀏覽次數 活動
3
3月 24
1832
1
7月 23
2656
2
3月 24
2020
3
12月 23
6673
3
8月 23
3340