odoo context上下文用法總結(jié)
環(huán)境
odoo-14.0.post20221212.tar
context用法總結(jié)
獲取上下文
# 返回字典數(shù)據(jù),等價(jià)于 self._context{'lang': 'en_US', 'tz': 'Europe/Brussels'} self._context
{'lang': 'en_US', 'tz': 'Europe/Brussels'} recordSet.env.context ?# 注意,上下文是和記錄集綁定的,上述的self也代表記錄集
self.env.context
設(shè)置上下文
Model.with_context([context][, **overrides])
?-> records[源代碼]
返回附加到擴(kuò)展上下文的此記錄集的新版本。
擴(kuò)展上下文是提供的合并了overrides
的context
,或者是合并了overrides
當(dāng)前context
# current context is {'key1': True}r2 = records.with_context({}, key2=True)# -> r2._context is {'key2': True}r2 = records.with_context(key2=True)# -> r2._context is {'key1': True, 'key2': True}
需要注意的是,上下文是和記錄集綁定的,修改后的上下文并不會(huì)在其它記錄集中共享
應(yīng)用場景示例
用于action,為關(guān)聯(lián)視圖添加默認(rèn)搜索、過濾條件
視圖定義
為設(shè)置action打開的tree列表視圖,添加默認(rèn)搜索,搜索條件為?state
字段值等于True
<odoo>
? ?<record id="link_estate_property_action" model="ir.actions.act_window">
? ? ? ?<field name="name">Properties</field>
? ? ? ?<field name="res_model">estate.property</field>
? ? ? ?<field name="view_mode">tree,form</field>
? ? ? ?<field name="context">{'search_default_state': True}</field>
? ?</record>
? ?<record id="estate_property_search_view" model="ir.ui.view">
? ? ? ?<field name="name">estate.property.search</field>
? ? ? ?<field name="model">estate.property</field>
? ? ? ?<field name="arch" type="xml">
? ? ? ? ? ?<search>
? ? ? ? ? ? ? ?<!-- 搜索 -->
? ? ? ? ? ? ? ?<field name="name" string="Title" /> ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?<separator/>
? ? ? ? ? ? ? ?<!-- 篩選 -->
? ? ? ? ? ? ? ?<filter string="Available" name="state" domain="['|',('state', '=', 'New'),('state', '=', 'Offer Received')]"></filter> ? ? ? ? ? ? ?
? ? ? ? ? ?</search>
? ? ? ?</field>
? ?</record>
? ?<!--此處代碼略...--></odoo>
說明:
<field name="context">{'search_default_fieldName': content}</field>
search_default_fieldName
,其中fieldName
?表示過濾器名稱,即搜索視圖中定義的<field>
、<filter>
元素的name
屬性值
content
?如果fieldName
為搜索字段<field>
的name
屬性值,那么content
表示需要搜索的內(nèi)容,輸入內(nèi)容是字符串,則需要添加引號(hào),形如'test'
;如果fieldName
為搜索過濾器<filter>
的name
屬性值,那么content
表示布爾值,該值為真,則表示默認(rèn)開啟name
所代表的過濾器,否則不開啟。
用于搜索視圖,添加分組查詢條件
視圖設(shè)計(jì)
<odoo>
? ?<!--此處代碼略...-->
? ?<record id="estate_property_search_view" model="ir.ui.view">
? ? ? ?<field name="name">estate.property.search</field>
? ? ? ?<field name="model">estate.property</field>
? ? ? ?<field name="arch" type="xml">
? ? ? ? ? ?<search> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?<!-- 分組 -->
? ? ? ? ? ? ? ?<group expand="1" string="Group By">
? ? ? ? ? ? ? ? ? ?<filter string="朝向" name="garden_orientation" context="{'group_by':'garden_orientation'}"/>
? ? ? ? ? ? ? ?</group>
? ? ? ? ? ?</search>
? ? ? ?</field>
? ?</record>
? ?<!--此處代碼略...--></odoo>
說明:'group_by': '分組字段名稱'
用于視圖對(duì)象按鈕,傳遞數(shù)據(jù)給模型方法
模型設(shè)計(jì)
#!/usr/bin/env python# -*- coding:utf-8 -*-from odoo import models, fields, apiclass EstatePropertyType(models.Model):
? ?_name = 'estate.property.type'
? ?_description = 'estate property type'
? ?name = fields.Char(string='name', required=True, help='help text')
? ?property_ids = fields.One2many('estate.property', 'property_type_id')
? ?offer_ids = fields.One2many('estate.property.offer', 'property_type_id')
? ?offer_count = fields.Integer(compute='_compute_offer_count')
? ?def _compute_offer_count(self): ? ? ? ?for record in self:
? ? ? ? ? ?record.offer_count = len(record.mapped('offer_ids.price')) ?
? ?def action_confirm(self, *args): ? ? ? ?print(self, self.env.context, args) ? ? ? ?# ... do something else
視圖設(shè)計(jì)
<odoo>
? ?<!--此處代碼略...-->
? ?<record id="estate_property_type_view_form" model="ir.ui.view">
? ? ? ?<field name="name">estate.property.type.form</field>
? ? ? ?<field name="model">estate.property.type</field>
? ? ? ?<field name="arch" type="xml">
? ? ? ? ? ?<form string="Property Type">
? ? ? ? ? ? ? ?<sheet>
? ? ? ? ? ? ? ? ? ?<!--此處代碼略...-->
? ? ? ? ? ? ? ? ? ?<field name="offer_count">
? ? ? ? ? ? ? ? ? ?<field name="property_ids">
? ? ? ? ? ? ? ? ? ? ? ?<tree string="Properties">
? ? ? ? ? ? ? ? ? ? ? ? ? ?<field name="name"/>
? ? ? ? ? ? ? ? ? ? ? ? ? ?<field name="expected_price" string="Expected Price"/>
? ? ? ? ? ? ? ? ? ? ? ? ? ?<field name="state" string="Status"/>
? ? ? ? ? ? ? ? ? ? ? ?</tree>
? ? ? ? ? ? ? ? ? ?</field>
? ? ? ? ? ? ? ? ? ?<footer>
? ? ? ? ? ? ? ? ? ? ? <button name="action_confirm" type="object" context="{'currentRecordID': active_id, 'offer_count':offer_count, 'property_ids': property_ids}" string="確認(rèn)" class="oe_highlight"/>
? ? ? ? ? ? ? ? ? ?</footer>
? ? ? ? ? ? ? ?</sheet>
? ? ? ? ? ?</form>
? ? ? ?</field>
? ?</record></odoo>
說明:context
屬性值中的字典的鍵值如果為模型中定義的字段名稱,則該字段名稱必須以<field>
元素的形式,出現(xiàn)在模型對(duì)應(yīng)的視圖(即不能是內(nèi)聯(lián)視圖,比如內(nèi)聯(lián)Tree列表)中,否則會(huì)出現(xiàn)類似錯(cuò)誤提示:
Field offer_count used in context.offerCount ({'offerCount': offer_count}) must be present in view but is missing.
點(diǎn)擊界面按鈕后,服務(wù)端打印日志如下
estate.property.type() {'lang': 'en_US', 'tz': 'Europe/Brussels', 'uid': 2, 'allowed_company_ids': [1], 'params': {'action': 165, 'cids': 1, 'id': 1, 'menu_id': 70, 'model': 'estate.property.type', 'view_type': 'form'}, 'currentRecordID': 1, 'offer_count': 4, 'property_ids': [[4, 49, False], [4, 48, False]]} ([1],)
說明:args
?從日志來看,args
接收了當(dāng)前記錄ID
注意:
如果將
def action_confirm(self, *args)
?改成def action_confirm(self, arg)
,服務(wù)端控制臺(tái)會(huì)收到類似如下告警(雖然點(diǎn)擊按鈕后,服務(wù)端不會(huì)拋異常):2023-02-06 01:28:53,848 28188 WARNING odoo odoo.addons.base.models.ir_ui_view: action_confirm on demo.wizard has parameters and cannot be called from a button
如果將
def action_confirm(self, *args)
改成def action_confirm(self)
,則點(diǎn)擊頁面確認(rèn)按鈕時(shí),服務(wù)端會(huì)報(bào)錯(cuò)誤,如下:TypeError: action_confirm2() takes 1 positional argument but 2 were given
用于視圖動(dòng)作按鈕,傳遞數(shù)據(jù)給動(dòng)作關(guān)聯(lián)的視圖
視圖設(shè)計(jì)
<odoo> ? ?
? ?<!--此處代碼略...-->
? ?<record id="estate_property_view_form" model="ir.ui.view">
? ? ? ?<field name="name">estate.property.form</field>
? ? ? ?<field name="model">estate.property</field>
? ? ? ?<field name="arch" type="xml">
? ? ? ? ? ?<form string="estate property form">
? ? ? ? ? ? ? ?<header>
? ? ? ? ? ? ? ? ? ?<button name="%(action_demo_wizard)d" type="action"
? ? ? ? ? ? ? ? ? ?string="選取offers" context="{'is_force':True}" class="oe_highlight"/>
? ? ? ? ? ? ? ? ? ?<!--此處代碼略...--> ? ?
? ? ? ? ? ? ? ?</sheet>
? ? ? ? ? ?</form>
? ? ? ?</field>
? ?</record> ? ? </odoo>
傳遞數(shù)據(jù)給視圖按鈕
action_demo_wizard
?action關(guān)聯(lián)視圖設(shè)計(jì)
<odoo>
? ?<data>
? ? ? ?<!--此處代碼略...--> ?
? ? ? ?<record id="demo_wizard_view_form" model="ir.ui.view">
? ? ? ? ? ?<field name="name">demo.wizard.form</field>
? ? ? ? ? ?<field name="model">demo.wizard</field>
? ? ? ? ? ?<field name="arch" type="xml">
? ? ? ? ? ? ? ?<form> ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ?<!--此處代碼略...--> ?
? ? ? ? ? ? ? ? ? ?<footer> ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? <button name="action_confirm" context="{'is_force':context.get('is_force')}" string="確認(rèn)" class="oe_highlight"/>
? ? ? ? ? ? ? ? ? ? ? ?<button string="關(guān)閉" class="oe_link" special="cancel"/>
? ? ? ? ? ? ? ? ? ?</footer>
? ? ? ? ? ? ? ?</form>
? ? ? ? ? ?</field>
? ? ? ?</record>
? ? ? ?<!-- 通過動(dòng)作菜單觸發(fā) -->
? ? ? ?<record id="action_demo_wizard" model="ir.actions.act_window">
? ? ? ? ? ?<field name="name">選取offers</field>
? ? ? ? ? ?<field name="res_model">demo.wizard</field>
? ? ? ? ? ?<field name="type">ir.actions.act_window</field>
? ? ? ? ? ?<field name="view_mode">form</field>
? ? ? ? ? ?<field name="target">new</field>
? ? ? ? ? ?<field name="binding_model_id" ref="estate.model_estate_property"/>
? ? ? ? ? ?<field name="binding_view_types">form</field>
? ? ? ?</record> ? ? ?
? ?</data></odoo>
傳遞數(shù)據(jù)給視圖關(guān)系字段
<odoo>
? ?<data>
? ? ? ?<!--此處代碼略...--> ?
? ? ? ?<record id="demo_wizard_view_form" model="ir.ui.view">
? ? ? ? ? ?<field name="name">demo.wizard.form</field>
? ? ? ? ? ?<field name="model">demo.wizard</field>
? ? ? ? ? ?<field name="arch" type="xml">
? ? ? ? ? ? ? ?<form>
? ? ? ? ? ? ? ? ? ?<field name="offer_ids" context="{'is_force':context.get('is_force')}" >
? ? ? ? ? ? ? ? ? ? ? ?<tree> ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ?<!--此處代碼略...-->
? ? ? ? ? ? ? ? ? ? ? ?</tree>
? ? ? ? ? ? ? ? ? ?</field>
? ? ? ? ? ? ? ? ? ?<!--此處代碼略...--> ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?</form>
? ? ? ? ? ?</field>
? ? ? ?</record>
? ? ? ?<!-- 通過動(dòng)作菜單觸發(fā) -->
? ? ? ?<record id="action_demo_wizard" model="ir.actions.act_window">
? ? ? ? ? ?<field name="name">選取offers</field>
? ? ? ? ? ?<field name="res_model">demo.wizard</field>
? ? ? ? ? ?<field name="type">ir.actions.act_window</field>
? ? ? ? ? ?<field name="view_mode">form</field>
? ? ? ? ? ?<field name="target">new</field>
? ? ? ? ? ?<field name="binding_model_id" ref="estate.model_estate_property"/>
? ? ? ? ? ?<field name="binding_view_types">form</field>
? ? ? ?</record> ? ? ?
? ?</data></odoo>
用于視圖關(guān)系字段,傳遞數(shù)據(jù)給模型方法
模型設(shè)計(jì)
#!/usr/bin/env python# -*- coding: utf-8 -*-from odoo import models, fieldsclass EstateProperty(models.Model):
? ?_name = 'estate.property'
? ?_description = 'estate property table'
? ?name = fields.Char(required=True)
? ?property_type_id = fields.Many2one("estate.property.type", string="PropertyType", options="{'no_create_edit': True}")
? ?offer_ids = fields.One2many("estate.property.offer", "property_id", string="PropertyOffer") ? ?
? ?# ...此處代碼略 ?
? ?# 重寫父類read方法
? ?def read(self, fields=None, load='_classic_read'): ? ? ? ?print(self.env.context)
? ? ? ?property_type_id = self.env.context.get('propertyTypeId') ? ? ? ?if property_type_id: ? ? ? ? ? ?print('do something you want') ? ? ? ?return super(EstateProperty, self).read(fields, load) ?
視圖設(shè)計(jì)
<odoo>
? ?<!--此處代碼略...-->
? ?<record id="estate_property_type_view_form" model="ir.ui.view">
? ? ? ?<field name="name">estate.property.type.form</field>
? ? ? ?<field name="model">estate.property.type</field>
? ? ? ?<field name="arch" type="xml">
? ? ? ? ? ?<form string="Property Type">
? ? ? ? ? ? ? ?<sheet>
? ? ? ? ? ? ? ? ? ?<!--此處代碼略...-->
? ? ? ? ? ? ? ? ? ?<field name="property_ids" context="{'propertyTypeId': active_id}">
? ? ? ? ? ? ? ? ? ? ? ?<tree string="Properties">
? ? ? ? ? ? ? ? ? ? ? ? ? ?<field name="name"/>
? ? ? ? ? ? ? ? ? ? ? ?</tree>
? ? ? ? ? ? ? ? ? ?</field>
? ? ? ? ? ? ? ? ? ?<!--此處代碼略...-->
? ? ? ? ? ? ? ?</sheet>
? ? ? ? ? ?</form>
? ? ? ?</field>
? ?</record></odoo>
打開上述視圖(即加載內(nèi)聯(lián)Tree視圖)時(shí),會(huì)自動(dòng)調(diào)用estate.property
模型的read
方法,服務(wù)端控制臺(tái)輸出如下:
{'lang': 'en_US', 'tz': 'Europe/Brussels', 'uid': 2, 'allowed_company_ids': [1], 'params': {'action': 165, 'cids': 1, 'id': 1, 'menu_id': 70, 'model': 'estate.property.type', 'view_type': 'form'}, 'propertyTypeId': 1}do something you want
更多示例可參考文檔:[odoo 為可編輯列表視圖字段搜索添加查詢過濾條件](odoo 為可編輯列表視圖字段搜索添加查詢過濾條件.md)
用于記錄集,傳遞數(shù)據(jù)給模型方法
模型設(shè)計(jì)
#!/usr/bin/env python# -*- coding:utf-8 -*-from odoo import models, fields,apiclass EstatePropertyTag(models.Model):
? ?_name = 'estate.property.tag'
? ?_description = 'estate property tag'
? ?name = fields.Char(string='tag', required=True)
? ?color = fields.Integer(string='Color')
? ?def create(self, vals_list): # 通過重寫模型的create或者write方法,調(diào)用該方法前修改上下文,然后在方法中通過self.env.context獲取上下文中的目標(biāo)key值,進(jìn)而實(shí)現(xiàn)目標(biāo)需求
? ? ? ?res = super(EstatePropertyTag, self).create(vals_list) ? ? ? ?# 獲取上下文目標(biāo)key值
? ? ? ?if not self.env.context.get('is_sync', True): ? ? ? ? ? ?# do something you need
? ? ? ?return res
'estate.property.tag'].with_context(is_sync=False).create({'name': 'tag4', 'color': 4}).env.context
{'lang': 'en_US', 'tz': 'Europe/Brussels', 'is_sync': False}
self.env[