Python 3.x 使用_context()可以执行什么操作?当我使用上下文传递时,它调用哪个方法?

Python 3.x 使用_context()可以执行什么操作?当我使用上下文传递时,它调用哪个方法?,python-3.x,python-2.7,odoo-9,odoo-10,odoo-11,Python 3.x,Python 2.7,Odoo 9,Odoo 10,Odoo 11,有谁能告诉我,在奥多中,语境到底是什么? 如果我想从价目表中获取价格,我会编写如下代码 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,

有谁能告诉我,在奥多中,语境到底是什么? 如果我想从价目表中获取价格,我会编写如下代码

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\u id.with\u context()
到底要做什么才能给我定价呢。它调用哪种方法来获取价格?

首先,您必须知道odoo中的self.env.context是什么

上下文是一个python字典,用于将某些数据传递给一个方法,其类型是FrozenDict inmutable


当调用方法时,您需要更新或添加一个新的键来取消上下文,您必须使用上下文。

上下文用于将参数从xml传递到python的方法调用 许多odoo方法根据上下文中的键更改其值:

例如:

#此返回所有位置的产品可用数量
数量=产品数量\可用数量
#此退货可用于特定位置的数量
数量=产品。带有上下文(“位置”:位置id)。可用数量
with_context
用于添加或更新上下文的键

有关上下文的更多说明,请参见: