【问题标题】:How to set computed one2many field calculation odoo如何设置计算one2many字段计算odoo
【发布时间】:2016-11-23 06:25:19
【问题描述】:

我只想知道如何使用计算域计算来设置 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 树视图时,都会正确调用该函数。但是没有结果。提前致谢。

【问题讨论】:

    标签: python-2.7 openerp odoo-8 odoo-9


    【解决方案1】:

    在您的代码中,您没有指定记录到哪个产品 属于,这就是为什么这条记录没有被映射到任何 one2many 记录,因为没有定义 many2one 引用。意味着你需要 在 minmax.monthly.data 模型中设置 month_id 然后你可以 查看 product.product 中 one2many 字段中的数据。

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

    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函数字段中直接分配对象,我们不需要像旧版本那样返回字典。

    这些功能字段可以存储到数据库中,因此只需要在字段定义中设置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 字段可以由 odoo 框架自动管理,当你设置 记录中的 many2one 参考,然后逆模型有 one2many 关系并将自动管理(需要定义 One2many 字段)。但是你可以管理 One2many 也是如此,这是双向的,因此您需要管理其中一个 many2one 或 one2many。

    【讨论】:

    • 同样的结果。 minmax_monthly_data 表中没有存储数据。感谢您的回复。还有什么办法吗?
    • 您错过了设置 many2one 参考,请参阅我已更新功能。在您在 minmax.monthly.data 模型中设置 many2one 引用之前,它不会在 One2many 模型中显示。
    • 物理记录在那里您可以检查到数据库中。有一条记录,但与任何产品都不相关。
    • 酷。当我添加 store=True 属性时它工作正常。但是只有在我更新了依赖字段时才会触发该功能。在初始模块安装中不调用它。我可以在模块安装中更新这个 one2many 字段吗?
    • 先在py中注释掉该字段,然后重启服务器和升级模块,然后取消注释该字段,然后再次重启服务器和升级模块。
    猜你喜欢
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多