Search code examples
python-2.7odooodoo-8odoo-9

How to set computed one2many field calculation odoo


I just want to know how to set the values in one2many by using computed field calculation. Here my code is

python file

from openerp import models ,fields,api
from openerp import SUPERUSER_ID
from dateutil.relativedelta import relativedelta
import openerp.addons.decimal_precision as dp
import math
import logging
import datetime

class extend_product_product(models.Model):
    _inherit = 'product.product'

    monthly_lines = fields.One2many('minmax.monthly.data','month_id', 'Monthy Sales',compute="_get_monthly_sales")

 @api.one
 def _get_monthly_sales(self):
    vals = {}
    vals.update({'monthly_lines':[(0,0,{'month_name_id':1,'so_qty':35})]})
    self.write(vals) # Also I have tried self.monthly_lines = [(0,0,{'month_name_id':1,'so_qty':35})]

class minmax_monthly_data(models.Model):
    _name = 'minmax.monthly.data'

    month_id = fields.Many2one('product.product', 'Product Reference', select=True, required=True)
    month_name_id = fields.Many2one('minmax.months','Minmax Month',required=True)
    so_qty = fields.Float('Sales Qty', digits_compute=dp.get_precision('Product Unit of Measure'), required=True)

XML File

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
      <record model="ir.ui.view" id="product_monthly_minmax_tree">
            <field name="name">product.product.tree</field>
            <field name="model">product.product</field>
            <field name="inherit_id" ref="product.product_product_tree_view"/>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <field name="ean13" position="after">
                    <field name="monthly_lines" widget="one2many_tags" />
                </field>
            </field>
        </record>
    </data>
</openerp>

Here I have tried to insert the data manually. The function is called properly whenever we load the product.product tree view. But there is no results. Thanks in advance.


Solution

  • In your code you have not specified to which product that record belongs, this is why this record is not getting mapped to any one2many records because there is no many2one reference defined. Means you need to set month_id in minmax.monthly.data model and then you can see the data in one2many field in product.product.

    Your code should be like that...

    class extend_product_product(models.Model):
        _inherit = 'product.product'
    
        monthly_lines = fields.One2many('minmax.monthly.data','month_id', 'Monthy Sales',compute="_get_monthly_sales")
    
        @api.multi
        def _get_monthly_sales(self):
            for rec in self:
                rec.monthly_lines = [(0,0,{'month_name_id':1,'so_qty':35, 'month_id' : rec.id})]
    

    In new api functional field directly be assigned with the object, we don't need to return dictionary as we did in old version.

    These functional fields can be stored into the database, for that you need to set the store=True attribute only in field definition.

    And if you store the field in database then in such situation we need to update it's value as well, for that you need to decorate the function with @api.depends('field name1','field name2')

    @api.multi
    @api.depends('fieldname')
    def _get_monthly_sales(self):
        for rec in self:
            rec.monthly_lines = [(0,0,{'month_name_id':1,'so_qty':35,'month_id' : rec.id})]
    

    One2many fields can be auto managed by odoo framework, when you set many2one reference in record then the inverse model have the one2many relation and that will be managed auto (One2many field needed to be defined). However you can manage One2many as well, this is bi-directional, so you need to manage either many2one or one2many.