gpt4 book ai didi

python-2.7 - 如何设置计算的one2many字段计算odoo

转载 作者:行者123 更新时间:2023-12-04 17:55:12 24 4
gpt4 key购买 nike

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

最佳答案

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功能字段中直接赋值给对象,我们不需要像旧版本那样返回字典。

这些功能字段可以存储到数据库中,为此你只需要在字段定义中设置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.

关于python-2.7 - 如何设置计算的one2many字段计算odoo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40757458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com