如何在odoo9中传递上下文?

时间:2016-08-19 12:36:16

标签: python openerp odoo-9

我想通过上下文将在销售订单中选择的partner_id传递给价格表怎么做?

@api.multi
@api.onchange('product_id')
def product_id_change(self):
    if not self.product_id:
        return {'domain': {'product_uom': []}}

    vals = {}
    domain = {'product_uom': [('category_id', '=', self.product_id.uom_id.category_id.id)]}
    if not self.product_uom or (self.product_id.uom_id.category_id.id != self.product_uom.category_id.id):
        vals['product_uom'] = self.product_id.uom_id

    product = self.product_id.with_context(
        lang=self.order_id.partner_id.lang,
        partner=self.order_id.partner_id.id,
        quantity=self.product_uom_qty,
        date=self.order_id.date_order,
        pricelist=self.order_id.pricelist_id.id,
        uom=self.product_uom.id
    )

    name = product.name_get()[0][1]
    if product.description_sale:
        name += '\n' + product.description_sale
    vals['name'] = name

    self._compute_tax_id()

    if self.order_id.pricelist_id and self.order_id.partner_id:
        vals['price_unit'] = self.env['account.tax']._fix_tax_included_price(product.price, product.taxes_id, self.tax_id)
    self.update(vals)
    return {'domain': domain}

@api.onchange('product_uom', 'product_uom_qty')
def product_uom_change(self):
    if not self.product_uom:
        self.price_unit = 0.0
        return
    if self.order_id.pricelist_id and self.order_id.partner_id:
        product = self.product_id.with_context(
            lang=self.order_id.partner_id.lang,
            partner=self.order_id.partner_id.id,
            quantity=self.product_uom_qty,
            date_order=self.order_id.date_order,
            pricelist=self.order_id.pricelist_id.id,
            uom=self.product_uom.id,
            fiscal_position=self.env.context.get('fiscal_position')
        )
        self.price_unit = self.env['account.tax']._fix_tax_included_price(product.price, product.taxes_id, self.tax_id)

这里

vals['price_unit'] = self.env['account.tax']._fix_tax_included_price(product.price, product.taxes_id, self.tax_id)

我想传递partner_id上下文,以便我可以检查特定的Bool是True还是false并根据它进行计算。

当我通过上下文时,它说这接受4并且我给了5。 现在我在哪里改变它所以它需要5。

1 个答案:

答案 0 :(得分:1)

由于新的API上下文封装在环境对象中,通常可以像self.env一样调用。要操纵上下文,只需使用方法with_context。一个简单的例子:

vals['price_unit'] = self.env['account.tax']\
    .with_context(partner_id=self.order_id.partner_id.id)\
    ._fix_tax_included_price(
        product.price, product.taxes_id, self.tax_id)

有关详细信息,请查看Odoo Doc