如何用onchange获取id进行过滤

时间:2017-11-05 15:55:10

标签: openerp

如何从另一个模型中检索many2one字段或其ID的值 例如:

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')

5000分之91 我希望当我更改contrat_parent_id时,我会将其恢复使用并过滤我的文章以获取字段'articlecontrat_ids'

1 个答案:

答案 0 :(得分:2)

这里你需要使用onchange事件我假设facturation.articleouvrage有一个名为contrat_id的m2o字段

# 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时删除所有记录,但我认为您将用户设为ungry 重新选择所有这些记录。

    self.articlecontrat_ids = [(5, 0, 0)]