獲得整個context函數
self.context_get() self.env['res.users'].context_get()
獲得context裏面對應的值ui
eg:獲得flag的值spa
self.env.context.get('flag',False)
修改context裏面的對應的值code
self.with_context({'flag': False}) 或 self.with_context(flag=True) 單一時 或 ctx = dict(context or {}) ctx['flag']=False self.with_context(ctx)
保證context的字典類型
context = dict(context or {})orm
複製contextblog
c = context.copy()
主要改變部分值,帶到其它函數中get
本身構造contextstring
context = {'flag':Fasle,'lang':'zh_cn'}
常規用法
帶入函數中it
if part.lang: self = self.with_context(lang=part.lang) product = self.env['product.product'].browse(product) ------ return self.with_context(ctx).write({'invoice_line': []})
記住,用了 with_contextio
視圖中引入context
model="ir.actions.act_window"時
<field name="context">{"search_default_my_export_list":1} </field>
表明 搜索時 my_expert_list 值爲1 對於 search_default_是系統的前置標識
分組
<filter string="Day"name="group_day"context="{'group_by':'date_start:day'}"
指定搜索
<field name="department_id" string="Department" context="{'invisible_department': False}"/>
列表中字段
<field name="line_ids" context="{'currency_id': currency_id,'default_analytic_account': context.get('analytic_account', '')}">
視圖定義context 帶入函數
<field name="product_id" on_change="onchange_product_id(product_id, context)" context="{'default_hr_expense_ok':1}"/>
many2one字段指定打開視圖:
tree_view_ref,form_view_ref,……
<field name="stock_quant_id" required="1" options="{'no_create_edit': True}" context="{'tree_view_ref':'tjc_stock.tjc_view_stock_quant_tree'}"/>