2

I want to create 2 fields in model namely field_a, and field_b such that:

  • when field_a's value is changed, change value of field_b using value of field_a in the calculation
  • when field_b's value is changed, change value of field_a using value of field_b in the calculation

I tried using onchange on both fields to update each other's value, but it results in a pretty ugly bug that inconsistently updates the values.

I would like to know a solution that works consistently.

Note: Both fields will be used by other fields as well.

holydragon
  • 6,158
  • 6
  • 39
  • 62

1 Answers1

3

You can use @api.depends here and define a compute method for each field.

field_a = fields.Char()
field_b = fields.Char()

@api.depends('field_b')
def get_updated_value_from_b(self):
    # Your logic to change fields values depending on other
    # assign updated values to your fields
    field_a = new_value_a

@api.depends('field_a')
def get_updated_value_from_a(self):
    # Your logic to change fields values depending on other
    # assign updated values to your fields
    field_b = new_value_b
Kenly
  • 24,317
  • 7
  • 44
  • 60
Pruthvi Barot
  • 1,948
  • 3
  • 14
  • I added more detail about the changed values. The values are calculation depending on each other's value as well. I tried your code but it is somehow giving inconsistent results similar to what I did. Sorry for the misinformation. – holydragon Jun 28 '21 at 08:10
  • Updated my snippet, can you try this? – Pruthvi Barot Jun 28 '21 at 08:16