2016-06-07 175 views
0

在顧客發票賬戶模塊中獲取價值是有one2many場,如何從one2many場odoo

invoice_line = fields.One2many('account.invoice.line', 'invoice_id', string='Invoice Lines') 

利用這個字段,我們可以在發票上添加多個產品。添加多個產品後,如何將這些產品與該領域分離,以便我可以獲得產品ID。

假設如果我們保存兩個產品,我們將有兩個條目。從這兩項我需要分開每個產品的產品編號

+0

您想從中獲得這些產品ID? – qvpham

+0

使用此字段我們將保存產品。從這個產品我想要產品ID –

+0

我不明白,你想如何使用它。但是這個字段建立了與'account.invoice.line'的關係。在'account.invoice.line'中與'product.product'有關係。所以你需要用這個'invoice_id'獲取對象'account.invoice.line'並獲取他的數據。在這個數據中有一個字段'product_id' – qvpham

回答

4

對於你的問題,我只能給你一個普遍的答案。我希望,你可以從它開始。

在odoo模型(osv.osv,...)中,您可以使用self.pool.get("model name")來獲取任何模型的對象池。有了這個池,你可以使用方法read()來讀取數據。
Odoo模型主要存儲在數據庫的一個表中。

首先,您需要了解Odoo中對象的關係。你的情況是這樣:

account.invoice --(invoice_line_ids:one2many)--> account.invoice.line --(product:many2one)-> product 
  • 閱讀one2many場的返回目標對象的ID的list
  • 的讀取Many2one字段返回目標對象的ID的值int

這裏是從發票的線得到的產品ID的示例:

# call object pool for account.invoice 
invoice_pool = self.pool.get("account.invoice") 

# here you need the invoice_id to get the data. 
# you can get it by parsing the parameter context 
found_invoices = invoice_pool.read(cr, uid, [invoice_id,], ["invoice_line_ids"], context) 

# It returns a list, but i gave only one invoice_id. 
# the list has maximun one element. we need the frist element 
found_invoice = found_invoices[0] if found_invoices else None 

invoice_line_ids = found_invoice["invoice_line_ids"] 

# the same with "account.invoice.line" 
invoice_line_pool = self.pool.get("account.invoice.line") 
invoice_lines = invoice_line_pool.read(cr, uid, invoice_line_ids, ["product_id"], context) 

# Here you have the product ids 
# I don't need to get the first element, because it returns a int 
product_ids = [line["product_id"] for line in invoice_lines] 

cruidcontext是參數,它從一個請求獲得。您可以通過覆蓋方法read,write,... 重要提示:您需要invoice_id才能啓動。您可以通過解析變量context來獲得該值。

您可以使用logging顯示的context在日誌文件中的內容:

import logging 

_logger = logging.getLogger(__name__) 
_logger.info("context type: " + type(context)) 
_logger.info("context content: " + str(context)) 

P/S:您將需要定製我的代碼,以配合你的,因爲我不知道很多有關你的想法。我正在與Odoo 9合作。但它與Odoo 8的核心基本相同