Use inverse and compute

I am trying to use the inverse in my variable but it does not execute anything, I may misunderstand it but it is assumed that if I occupy the inverse in a computed field I will take what I do in the inverse according to the restriction?

I want a field that is computed to be copied with another variable in case it is 0.

Here is my code:

  total_debit = fields.Float (string = "Input value", compute = "_total_a_debit", inverse = "_inverse_debit", store = True)

  total_credit = fields.Float (string = "Output value", compute = "_total_a_credit", store = True)

  @api.one
  @api.depends("total_debit", "total_credit")
  def _inverse_debit(self):
      for rec in self:
          if rec.total_debit == 0.0 :
              rec.total_debit = rec.total_credit

Upvotes: 1

Views: 68

Answers (1)

Adam Strauss
Adam Strauss

Reputation: 1999

The documentation says:

The inverse method, as its name says, does the inverse of the compute method: the invoked records have a value for the field, and you must apply the necessary changes on the field dependencies such that the computation gives the expected value.

Note that a computed field without an inverse method is read-only by default.

Example:

document = fields.Char(compute='_get_document', inverse='_set_document')


def _get_document(self):
    for record in self:
        with open(record.get_document_path) as f:
            record.document = f.read()
def _set_document(self):
    for record in self:
        if not record.document: continue
        with open(record.get_document_path()) as f:
            f.write(record.document)

In your case:

total_debit = fields.Float(string="Input value", compute="_total_a_debit", inverse="_inverse_debit", store=True)

total_credit = fields.Float(string="Output value", compute="_total_a_credit", store=True)


def _inverse_debit(self):
    for rec in self:
        if rec.total_debit == 0.0 :
            rec.total_debit = rec.total_credit

Upvotes: 3

Related Questions