如何在odoo9中传递上下文?

How to pass context in odoo9?

我想通过上下文将在销售订单中选择的 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 是真还是假,并根据它进行计算。

当我传递上下文时,它说它接受 4,而我给了 5。 现在我要在哪里更改它,所以它需要 5.

由于新的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