2016-11-23 122 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

物理記錄是否存在,您可以檢查數據庫。有一條記錄,但與任何產品無關。 –