Ma7
Ma7

Reputation: 229

How to filter Many2one value depend another field?

please I have a custom module here is a capture :

enter image description here

then I go to Sales order and modify the module sale.order.line i add some fields in relation with my custom module

enter image description here

Now my request is in ligne contrat i want only lignes in the contrat

for example if i choose Contrat 01 only ligne in Contrat 01 like this

enter image description here

here is my code :

enter image description here

enter image description here

Upvotes: 1

Views: 1147

Answers (2)

Charif DZ
Charif DZ

Reputation: 14721

What @djames did will work only in this form view if you want to have this behavior in all your sale.order.line views use python to do this job for you.

 class bons_lines(model.Model):
      _inherit = 'sale.order.line'

      # your new fields
      ....
      ....


      @api.onchange('contrat_name_id')
      def onchange_contrat_name(self):
          if self.contrat_name_id:
              # add the domain
              self.contrat_lignes_id = False # force the user to reselect the contrat_lignes_id if he changes the contrat name
              return {'domain': {'contrat_lignes_id': [('ligne_ids', '=', self.contrat_name_id.id)]}}
          else:
              # remove the domain 
              return {'domain': {'contrat_lignes_id': []}}

This way you will not have to add the domain in every XML view you declare.

Upvotes: 1

djames
djames

Reputation: 119

You can use a domain in the field definition in your XML:

<field name="contrat_name_id"/>
<field name="contrat_lignes_id" domain="[('ligne_ids', '=', contrat_name_id)]"/>

This will filter contrat_lignes_id to only show records where ligne_ids matches what you entered for contrat_name_id on that line.

Upvotes: 2

Related Questions