get_vendcontact_ids = fields.Many2many('my.contact', compute='_get_vendcontact_ids')
@api.one
def _get_vendcontact_ids(self):
if self.vendor_id:
self.get_vendcontact_ids = self.vendor_id.contact_ids.mapped('id')
else:
self.get_vendcontact_ids = None
<field name="get_vendcontact_ids" attrs="{'invisible':True}"/>
vendcontact_id = fields.Many2one('my.contact', 'Контакт поставщика', domain="[('id','in', get_vendcontact_ids and get_vendcontact_ids[0][2])]")
@api.onchange('vendor_id')
def onchange_vendor(self):
if self.vendor_id:
vendor_contact_ids = self.vendor_id.contact_ids.mapped('id')
self.get_vendcontact_ids = self.vendor_id.contact_ids.mapped('id') # Обновление списка id для домена
# Очистить поле с контактом, если он не принадлежит выбранному поставщику
if self.vendcontact_id and self.vendcontact_id not in vendor_contact_ids:
self.vendcontact_id = None
else:
self.get_vendcontact_ids = None
self.vendcontact_id = None
<field name="vendcontact_id" domain="[('id', 'in', [get_vendcontact_ids])]"/>
...
active_id = context.get('active_id', False) # <-- Падает тут, говорит, что нет env
vendor = self.pool.get('my.agreement').browse(active_id).vendor_id
]]>
def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
if context is None:
context = {}
res = super(my.agreement, self).fields_view_get(cr, user, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
if view_type == 'form':
active_id = self.env.context.get('active_id', False) # <-- Падает тут, говорит, что нет env
vendor = self.env['my.agreement'].browse(active_id).vendor_id
vendor_contact_ids = self.vendor_id.contact_ids.mapped('id')
for field in res['fields']:
if field == 'vendcontact_id':
res['fields'][field]['domain'] = [('id', 'in', vendor_contact_ids)]
return res
]]>