我只想知道如何使用计算字段计算来设置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树视图时,都会正确地调用该函数。但没有结果。提前谢谢。
发布于 2016-11-23 10:17:05
在您的代码中,您没有指定该记录属于哪个产品,这就是为什么该记录没有映射到任何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字段可以由自动管理,当您在记录中设置many2one引用时,逆模型具有one2many关系,并且将被管理为auto (需要定义One2many字段)。但是您也可以管理one2many,这是双向的,所以您需要管理many2one或One2many。
https://stackoverflow.com/questions/40757458
复制相似问题