with_context() 到底是做什么的?当我通过 with_context 时它调用哪个方法?
What with_context() exectly do ? which method it call when i pass with_context?
谁能告诉我 with_context 在 odoo 中到底做了什么?
如果假设我想从价目表中获取价格,我会像这样编写代码。
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=today_date,
pricelist=self.order_id.pricelist_id.id,
uom=self.product_uom.id,
fiscal_position=self.env.context.get('fiscal_position'))
price_unit = self._get_display_price(product)
@api.multi
def _get_display_price(self, product):
# TO DO: move me in master/saas-16 on sale.order
if self.order_id.pricelist_id.discount_policy == 'with_discount':
return product.with_context(pricelist=self.order_id.pricelist_id.id).price
final_price, rule_id = self.order_id.pricelist_id.get_product_price_rule(self.product_id, self.product_uom_qty or 1.0, self.order_id.partner_id)
context_partner = dict(self.env.context, partner_id=self.order_id.partner_id.id, date=self.order_id.date_order)
base_price, currency_id = self.with_context(context_partner)._get_real_price_currency(self.product_id, rule_id, self.product_uom_qty, self.product_uom, self.order_id.pricelist_id.id)
if currency_id != self.order_id.pricelist_id.currency_id.id:
base_price = self.env['res.currency'].browse(currency_id).with_context(context_partner).compute(base_price, self.order_id.pricelist_id.currency_id)
# negative discounts (= surcharge) are included in the display price
return max(base_price, final_price)
那么 product = self.product_id.with_context()
究竟做了什么才能让我拿到价格。它调用哪个方法来获取价格?
首先你必须知道 self.env.context 在 odoo
中是什么
上下文是一个python字典,用于将某些数据传递给方法,他的类型是FrozenDict不可变的。
当你想在调用方法时更新或添加新键到 de context 时,你必须使用 with_context。
上下文用于在从 xml 到 python 的方法调用之间传递参数
许多 odoo 方法根据上下文中的键更改它们的值:
示例:
# this return available quantity of the product in all location
quantity = product.qty_available
# this return avaible quantity for a specific location
quantity = product.with_context('location' : location_id).qty_available
和with_context
用于添加或更新上下文的键
有关上下文的更多解释,请参阅:
谁能告诉我 with_context 在 odoo 中到底做了什么? 如果假设我想从价目表中获取价格,我会像这样编写代码。
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=today_date,
pricelist=self.order_id.pricelist_id.id,
uom=self.product_uom.id,
fiscal_position=self.env.context.get('fiscal_position'))
price_unit = self._get_display_price(product)
@api.multi
def _get_display_price(self, product):
# TO DO: move me in master/saas-16 on sale.order
if self.order_id.pricelist_id.discount_policy == 'with_discount':
return product.with_context(pricelist=self.order_id.pricelist_id.id).price
final_price, rule_id = self.order_id.pricelist_id.get_product_price_rule(self.product_id, self.product_uom_qty or 1.0, self.order_id.partner_id)
context_partner = dict(self.env.context, partner_id=self.order_id.partner_id.id, date=self.order_id.date_order)
base_price, currency_id = self.with_context(context_partner)._get_real_price_currency(self.product_id, rule_id, self.product_uom_qty, self.product_uom, self.order_id.pricelist_id.id)
if currency_id != self.order_id.pricelist_id.currency_id.id:
base_price = self.env['res.currency'].browse(currency_id).with_context(context_partner).compute(base_price, self.order_id.pricelist_id.currency_id)
# negative discounts (= surcharge) are included in the display price
return max(base_price, final_price)
那么 product = self.product_id.with_context()
究竟做了什么才能让我拿到价格。它调用哪个方法来获取价格?
首先你必须知道 self.env.context 在 odoo
中是什么上下文是一个python字典,用于将某些数据传递给方法,他的类型是FrozenDict不可变的。
当你想在调用方法时更新或添加新键到 de context 时,你必须使用 with_context。
上下文用于在从 xml 到 python 的方法调用之间传递参数 许多 odoo 方法根据上下文中的键更改它们的值:
示例:
# this return available quantity of the product in all location
quantity = product.qty_available
# this return avaible quantity for a specific location
quantity = product.with_context('location' : location_id).qty_available
和with_context
用于添加或更新上下文的键
有关上下文的更多解释,请参阅: