如何使用onchange获取ID以进行过滤 [英] how get id with onchange for filtering
问题描述
我如何从另一个模型中检索many2one字段的值或其ID 例如:
how can i retrieve the value of a many2one field or its ID from another model for exemple:
class Contrat(models.Model):
_name = 'facturation.contrat'
contrat_parent_id = fields.Many2one('facturation.contrat', string='Numéro Contrat Client',
domain=[('is_prestataire', '=', False)])
class Lot(models.Model):
contrat_id = fields.Many2one('facturation.contrat', ondelete='cascade')
articlecontrat_ids = fields.Many2many('facturation.articleouvrage',string='Article Lot')
91/5000 我希望当我更改"contrat_parent_id"时,我可以重新使用它并为字段"articlecontrat_ids"过滤我的文章
91/5000 i want that when i change contrat_parent_id i get it back to use it and filter my articles for field 'articlecontrat_ids'
推荐答案
在这里您需要使用onchange事件,我假设facturation.articleouvrage
有一个名为contrat_id
here you need to use onchange event i'm assuming that facturation.articleouvrage
have a m2o field named contrat_id
# in onchange event always put the name of the field that trigger the event
@api.onchange('contrat_parent_id ')
def onchange_contrat(self):
"""update the domain when we change the contrat"""
if self.contrat_parent_id :
# always check if the field is not empty
# return the domain like this but i don't know what you need exactly
return {'domain': {'articlecontrat_ids ' : [('contrat_id ', '=', self.contrat_parent_id.contract.id)]}}
else: # remove the domain
return {'domain': {'articlecontrat_ids ' : []}}
如果您要在用户更改contrat_id时删除所有记录,但我认为您会使用户感到很饿 重新选择所有这些记录.
if you want to remove all records when user change the contrat_id but i think you make the user ungry to reselect all this records.
self.articlecontrat_ids = [(5, 0, 0)]
这篇关于如何使用onchange获取ID以进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!