2016-11-23 310 views
2

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

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树视图,该函数都会正确调用。但没有结果。提前致谢。

回答

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,这是双向的,因此您需要管理any2one或one2many 。

+0

结果相同。没有数据存储在minmax_monthly_data表中。感谢您的回复。任何其他方式? – balaraman

+0

您错过了设置many2one参考,请参阅我已更新的功能。直到您在'minmax.monthly.data'模型中设置many2one引用,它才会在One2many模型中显示。 –

+0

物理记录是否存在,您可以检查数据库。有一条记录,但与任何产品无关。 –