2017-11-30 10 views
-2

誰もが、正確にwith_contextがodooで何をするのか教えていただけますか? 私が価格表から価格を取得したいと思うなら、私はこのようなコードをします。with_context()は何をしますか? with_contextを渡すとどのメソッドが呼び出されますか?

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()が私に値段をつけます。それは価格を得るために呼び出す方法は?

答えて

1

は、最初のあなたは、コンテキストがPythonの辞書で、メソッドに特定のデータを渡すために使用され、彼のタイプはFrozenDict inmutableあるodoo

であるかself.env.context知っている必要があります。

また、メソッドを呼び出すときにコンテキストに新しいキーを追加または追加する場合は、with_contextを使用する必要があります。

0

Contextは、メソッド呼び出しのparametreを渡すために使用されます。 paythonするXMLから 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追加またはコンテキストの更新キー

ためするために使用されコンテキストについての詳しい説明は次のとおりです。

what is context in odoo

関連する問題