How can I read from a computed field? In Odoo 9 - odoo

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.

Related

Counting Rows of one2many field odoo

How can I count the rows of a one2many field and output the current row count ?
This is how it should work.For example:
1 ------column1------------
2 ------column2------------
3 ------column3------------
4 ------Column4------------
etc..
I have made an automated action for this, but it does not work as intended:
The automated action refers to the model of the one2many field and is triggered when a record is created. The following Python code is executed:
for line in record.picking_id.move_line_ids_without_package:
for rec in str(record.x_studio_position):
record['x_studio_position'] = len(record.picking_id.move_line_ids_without_package)
What happens is the following for e.g. 4 columns
4 ------column1------------
4 ------column2------------
4------column3------------
4 ------column4------------
It will write the total number in each row instead of the current column number.
You set the position to the number of lines in the field move_line_ids_without_package, it will be the same for all lines.
You can use enumerate to get the line sequence
Example:
for index, line in enumerate(record.picking_id.move_line_ids_without_package):
line['x_studio_position'] = index + 1

How to get computed field value in search orm in odoo

I have defined a computed field with compute method in odoo 10 and now i want to get its value in search orm but its value remain False, and when I tried store=True its value not being changed.
if anyone has solution please let me know, I'll highly thankful.
My code is:
balance_amount = fields.Float(string="Balance Amount", compute='_compute_loan_amount')
#api.one
def _compute_loan_amount(self):
total_paid = 0.0
for loan in self:
for line in loan.loan_lines:
if line.paid:
total_paid += line.amount
balance_amount = loan.loan_amount - total_paid
self.total_amount = loan.loan_amount
self.balance_amount = balance_amount
self.total_paid_amount = total_paid
when i use search_countbelow:
loan_count = self.env['hr.loan'].search_count([('employee_id', '=', values['employee_id']), ('state', '=', 'approve'),
('balance_amount', '!=', 0)])
it always get count value even balance_amount equals to Zero
There are some things you have to do here.
Define recomputation dependencies and either use #api.multi and a for loop over self or use api.one and skip a for loop. If i understand your compute methode correct:
#api.multi
#api.depends('loan_lines.paid', 'loan_lines.amount', 'loan_amount')
def _compute_loan_amount(self):
for loan in self:
total_paid = 0.0
for line in loan.loan_lines:
if line.paid:
total_paid += line.amount
balance_amount = loan.loan_amount - total_paid
loan.total_amount = loan.loan_amount # ???
loan.balance_amount = balance_amount
loan.total_paid_amount = total_paid
Try to use a float rounding with e.g. 2 decimals, because floats can produce values like 0.000000000000000002 and that would break your search.
You either have to store the value or have to define a search method. Second approach is more difficult (in my experience).

Populate selection field from loop odoo 9

How populate selection field from loop?
Example:
date = fields.Selection([],string='Date')
list = []
for i in diff:
print(i) #return 07:00
self.date = list
You can get selection field key,value pair via following method.
result=self.env[model].fields_get([field_name])
key=False
if result and result.get(field_name) and result.get(field_name).get('selection'):
for dict_value in result.get(field_name).get('selection'):
print dict_value
This may help you.

How to use the displayed value on powerbuilder for computation

I just want to ask on how to solve my problem.
On my report window, 2 computed fields:
1. A field that computes for decimal and I am displaying it to two decimals only (no problem on the display).
2. On my other computed field, I use the result of the first computed field to be multiplied to a certain number. The problem is here, because the value being multiplied is not the value being displayed from the first field. Instead it uses the whole amount.
Scenario:
2,055,232.135 is the computed value and id displays 2,055,232.14 which is good.
But if i multiply it to 9 (2055232.135 * 9), the result is 18,497,089.215 which will be displayed as 18,497,089.22
The problem is I want that the displayed value (2,055,232.14) to be multiplied to 9 (2055232.14 * 9) which will then results to 18,497,089.26
I only wanted to achieve the 2nd value for the computed field so that if the user computes for it, it will be equal.
The following solution can be used in a computed field:
String( Truncate( 2055232.135 * 9, 2), "#,##0.00")

Read data from environment odoo 9

I'm try read all name from res.partner but always console return empty res.partner()
In database I have record where is company_id = 1
#api.multi
def test(self):
my_ids = self.env['res.partner'].search([('company_id','=','1')])
print(my_ids)