maharshi maharshi - 3 months ago 21
Python Question

How to pass context in odoo9?

I want to pass the partner_id which is selected in sale order to pricelist via context how to do that ?

@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)


here

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


i want to pass the partner_id context so i can check if a specific Bool is True or false and do the computation according to it.

when i pass the context it says this accepts 4 and i have given 5.
now where do i change this so it takes 5.

Answer

Since new API context is encapsulated in an environment object, which often can be called like self.env. To manipulate the context just use the method with_context. An simple example:

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)

For further information look into the Odoo Doc

Comments