首页 文章

如何从另一个模块访问一个模块的数据库

提问于
浏览
-4

我正在尝试在openerp中进行一些自定义 . 现在我只是尝试从一个模块到另一个模块进行一些数据流,但我完全感到困惑 . 请给出好的指示来做这样的事情 .

我有以下问题:

  • 如何从另一个模块访问一个模块中的数据 .

  • 如何获取特定商品的数据(例如,在销售中获取特定客户的销售发票数据) .

  • 使用循环如何使用这些全部来查找客户所有发票的总金额 .

1 回答

  • 2

    您的问题很一般,因此很难准确回答您的需求,但总的来说:

    1-要从任何模型获取记录,您必须首先将其汇集,然后使用ORM函数(浏览,读取,...)从中获取数据:

    obj = self.pool.get('my.model.name')
    records = obj.browse(cr, uid, ids, context)
    

    2-要获取特定数据,您可以使用域搜索来过滤数据:

    #return ids where customer field equal to 1
    res_ids = obj.search(cr, uid, [('customer','=', 1)], context) 
    
    #get records corresponding to res_ids
    records = obj.browse(cr, uid, res_ids, context)
    

    3-要获得总金额,您可以执行以下操作:

    def get_total_amount(self, cr, uid, customer_id, context=None):
        obj = self.pool.get('account.invoice')
        ids = obj.search(cr, uid, [('partner_id', '=', customer_id)], context)
        total = 0
        for(rec in obj.browse(cr, uid, ids, context)):
            total = total + rec.amount_total
        return total
    

    函数的参数和返回可能会根据您需要使用它的位置而更改 .

相关问题