如何设置计算的one2many字段计算odoo

How to set computed one2many field calculation odoo

我只是想知道如何使用计算字段计算来设置 one2many 中的值。我的代码是

python 文件

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 文件

<?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>

这里我尝试手动插入数据。每当我们加载 product.product 树视图时,都会正确调用该函数。但是没有结果。提前致谢。

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.

你的代码应该是这样的...

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})]

在新的 api 功能字段中直接分配给对象,我们不需要像在旧版本中那样 return 字典。

这些功能字段可以存储到数据库中,为此你只需要在字段定义中设置store=True属性。

并且如果您将字段存储在数据库中,那么在这种情况下我们也需要更新它的值,为此您需要使用 @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.