Read data from environment odoo 9 - odoo

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)

Related

copying field custom from purchase order line to stock move in odoo

I am using odoo 13. I have a custom field weight in Purchase order line.
I want to copying the value of this field to custom field in stock move.
I know that to transfer the value of the field from sale order to stock move I can do it in the following way.
class StockMoveLine(models.Model):
_inherit = 'stock.move'
weight = fields.Float(
compute='_compute_weight' )
def _compute_weight(self):
for move in self:
if not (move.picking_id and move.picking_id.group_id):
continue
picking = move.picking_id
sale_order = self.env['sale.order'].sudo().search([
('procurement_group_id', '=', picking.group_id.id)], limit=1)
# print(picking)
if sale_order:
for line in sale_order.order_line:
if line.product_id.id != move.product_id.id:
continue
move.update({
'weight': line.weight,
})
continue
else:
# move.update({
# 'weight': move.weight,
# })
However I find myself stuck in the else to pass the field from purchase to stock move
You can use the purchase_line_id field on stock.move which is link to purchase.order.line
Code - PoL Reference in Stock Move
If you're inheriting stock.move, then change your model name to class StockMove(models.Model): instead of class StockMoveLine(models.Model):.
Try using #api.depends on your compute function.

calculate new price_unit in sale order line

I have created a new custom module to include width and lenght of a product as attributes with custom values, and I need to update the price_unit of the sales order line with the current unit price multiplied by the (lenght*witdh) value, so I can have the price per surface unit in the product card, and the price per total surface in the sales order line.
I use the product configurator and this is my code:
from odoo import models, fields, api
# import pymsgbox
class aurea_calculated_field_line(models.Model):
_inherit = 'sale.order.line'
field_superficie = fields.Float('Superficie')
field_alto = fields.Integer('Alto')
field_ancho = fields.Integer('Ancho')
#api.onchange('field_alto', 'field_ancho', 'product_uom_qty', 'quantity')
def _value_pc5(self):
for record in self:
record.field_superficie = record.field_ancho * record.field_alto
self.price_unit = float(self.field_superficie) * self.product_id.lst_price
#api.onchange('product_id')
def _value_pc4(self):
if not self.product_custom_attribute_value_ids and not self.product_no_variant_attribute_value_ids:
return ""
for pacv in self.product_custom_attribute_value_ids:
if pacv.custom_product_template_attribute_value_id.display_name == 'Largo: Largo':
self.field_alto = pacv.custom_value
if pacv.custom_product_template_attribute_value_id.display_name == 'Ancho: Ancho':
self.field_ancho = pacv.custom_value
It works fine, but the problem is when I change the product quantity the unit price is reset to the product pricelist price.
Any suggestions?
Thanks in advance
I looked at this a bit. I didn't reproduce the problem but here is something you probably can work with.
In Odoo13 source code in file addons/sale/models/sale.py on line 1614 is a method which causes behavior you have problem with:
#api.onchange('product_uom', 'product_uom_qty')
def product_uom_change(self):
if not self.product_uom or not self.product_id:
self.price_unit = 0.0
return
if self.order_id.pricelist_id and self.order_id.partner_id:
product = self.product_id.with_context(
lang=self.order_id.partner_id.lang,
partner=self.order_id.partner_id,
quantity=self.product_uom_qty,
date=self.order_id.date_order,
pricelist=self.order_id.pricelist_id.id,
uom=self.product_uom.id,
fiscal_position=self.env.context.get('fiscal_position')
)
self.price_unit = self.env['account.tax']._fix_tax_included_price_company(self._get_display_price(product), product.taxes_id, self.tax_id, self.company_id)
This re-calculates unit price when product quantity is changed. You'd have to overwrite this method in your code. Easiest way would be add this to your aurea_calculated_field_line-class.
def product_uom_change(self):
return
But this needs a lot of testing to make sure that some other feature does not break because of this.

TF-Ranking transform data to ELWC - ExampleListWithContext form

I have read all the guides, videos, and everything, but I have no idea how to convert my feature set to an ELWC datasheet format for TF-Rank ListWise problem. There is no description of this structure.
For example, a students profile is:
Student ID age grade math% physics% english% art% math_competit language_competit Rank
14588 16 k12 98 67 88 100 first_place very_good 5
If I have 20 students in the same class, how can I transform this data to be able to make a listwise prediction for every grade ( theoretically in every grade has 3 class with 20 students)
ELWC format requires 'context' and 'example features'. Example features are features that are different for every item in a query list. Context features are ones which are dependent only on the query.
Therefore, every query will have a list of features for every item in the list (the Example features), and a single list of features for Context features.
To convert to ELWC format, start by gathering all the items for a given query. The code below shows a query with two items along with some context information. Use input_pb2.ExampleListWithContext() to create an instance of a ELWC formatter. Then all you have to do is feed in the context and examples.
Save using TFRecordWriter.
from tensorflow_serving.apis import input_pb2
import tensorflow as tf
def _float_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
context = {
'custom_features_1': _float_feature(1.0),
'utility': _int64_feature(1),
}
examples = [
{
'custom_features_1': _float_feature(1.0),
'custom_features_2': _float_feature(1.5),
'utility': _int64_feature(1),
},
{
'custom_features_1': _float_feature(1.0),
'custom_features_2': _float_feature(2.1),
'utility': _int64_feature(0),
}
]
def to_example(dictionary):
return tf.train.Example(features=tf.train.Features(feature=dictionary))
ELWC = input_pb2.ExampleListWithContext()
ELWC.context.CopyFrom(to_example(context))
for expl in examples:
example_features = ELWC.examples.add()
example_features.CopyFrom(to_example(expl))
print(ELWC)

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

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.

Is it possible to compute many2many field values without storing them?

I want to create a virtual Many2many field for informational purpose only. Is it possible to compute and populate Many2many field without storing the generated virtual records?
Yes, It is possible to create Virtual Many2many field for informational purpose. Please see the below sample code,
#api.one
#api.depends(
'move_id.line_id.account_id',
'move_id.line_id.reconcile_id.line_id',
'move_id.line_id.reconcile_partial_id.line_partial_ids',
)
def _compute_move_lines(self):
# Give Journal Items related to the payment reconciled to this invoice.
# Return partial and total payments related to the selected invoice.
self.move_lines = self.env['account.move.line']
if not self.move_id:
return
data_lines = self.move_id.line_id.filtered(lambda l: l.account_id == self.account_id)
partial_lines = self.env['account.move.line']
for data_line in data_lines:
if data_line.reconcile_id:
lines = data_line.reconcile_id.line_id
elif data_line.reconcile_partial_id:
lines = data_line.reconcile_partial_id.line_partial_ids
else:
lines = self.env['account.move.line']
partial_lines += data_line
self.move_lines = lines - partial_lines
move_lines = fields.Many2many('account.move.line', string='Entry Lines',
compute='_compute_move_lines')
You can see above example in /addons/account/account_invoice.py file in the odoo addons.