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

时间:2016-11-23 06:25:20

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

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

python文件

npm info it worked if it ends with ok
npm info using npm@2.15.11
npm info using node@v4.6.2
npm info prestart installation@1.0.0
npm info start installation@1.0.0

> installation@1.0.0 start /usr/src/app
> ./test-install-post-publish2.sh

Cloning into 'suman-test-projects'...
The authenticity of host 'github.com (192.30.253.113)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.253.113' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

npm info installation@1.0.0 Failed to exec start script
npm ERR! Linux 4.4.27-boot2docker
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v4.6.2
npm ERR! npm  v2.15.11
npm ERR! code ELIFECYCLE
npm ERR! installation@1.0.0 start: `./test-install-post-publish2.sh`
npm ERR! Exit status 128
npm ERR! 
npm ERR! Failed at the installation@1.0.0 start script './test-install-post-publish2.sh'.
npm ERR! This is most likely a problem with the installation package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     ./test-install-post-publish2.sh
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs installation
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! 
npm ERR!     npm owner ls installation
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /usr/src/app/npm-debug.log

XML文件

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)

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

1 个答案:

答案 0 :(得分: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})]
  

当您设置时,可以通过odoo框架自动管理One2many字段   记录中有多个参考,然后逆模型具有one2many   关系和将自动管理(One2many字段需要定义)。但是你可以管理   One2many,这是双向的,所以你需要管理   很多人或者一个人。