2016-06-07 173 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的核心基本相同