3

I have a computed field in a model called page_price.

Class Page(models.Model):
    page_price = fields.Float(compute='compute_page_price')

    def compute_page_price(self):
        self.page_price = 7  # this value is an example

If I show this field in a view, it shows 7.

The problem is when I try to get the value from another model.

Class Book(models.Model):
  book_price = fields.Float(compute='compute_book_price')

  def compute_book_price(self):
    # page_id has the value of a Page row id
    page_price = self.env['Page'].search([('id', '=', page_id)])[0].page_price
    self.book_price = page_price * 10

Here, the value of book_price is always 0 instead of 70.
The vaule of page_price inside the compute_book_price function is 0 instead of 7.
Why is that and how can I obtain the correct value?

Note: If the page_price field is defined as a Float field instead of a computed field, the result of book_price is 70.

MouTio
  • 1,249
  • 23
  • 45
  • 1
    self.book_price = float(page_price) * 10 or try to print the value of page_price in function compute_book_price(self): and check what its return – Vigneshwaran Thenraj Jun 21 '17 at 09:44
  • 1
    Actually Compute function should be called above the fields declaration – Vigneshwaran Thenraj Jun 21 '17 at 09:45
  • @VigneshwaranThenraj , in the new API you can call the compute function after the fields declaration: http://odoo-new-api-guide-line.readthedocs.io/en/latest/fields.html#computed-fields I have print the page_price value inside compute_book_price and it is 0 – MouTio Jun 21 '17 at 09:54
  • I have also same problem, Did you get any solution `Mou Tio` ??? – M Tahir Noor Apr 26 '18 at 06:36
  • @TahirNoor I am afraid I have no solution yet. – MouTio Apr 26 '18 at 10:28
  • Please have a look at it. it may help you `https://stackoverflow.com/questions/50035832/how-to-get-computed-field-value-in-search-orm-in-odoo` – M Tahir Noor Apr 27 '18 at 07:11

0 Answers0