问题
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 i want that when i change contrat_parent_id i get it back to use it and filter my articles for field 'articlecontrat_ids'
回答1:
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 ' : []}}
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)]
来源:https://stackoverflow.com/questions/47123563/how-get-id-with-onchange-for-filtering