1.在Action中定义,context用于传递搜索条件和分组条件,在搜索视图中默认显示:

示例代码:

        <record model="ir.actions.act_window" id="open_company_allocation"><field name="name">Leaves Summary</field><field name="res_model">hr.holidays</field><field name="view_type">form</field><field name="view_mode">tree,form</field><field name="view_id" eval="view_holiday_simple"/><field name="context">{'search_default_group_type': 1, 'search_default_validated': 1}</field><field name="domain">[('holiday_type','=','employee')]</field><field name="search_view_id" ref="view_hr_holidays_filter"/></record>

View Code

其中:

<field name="context">{'search_default_group_type': 1, 'search_default_validated': 1}</field>
定义了窗口打开时默认的分组条件 group_type 和过滤条件 validated ,如图所示:

对应的搜索视图代码:

        <record id="view_hr_holidays_filter" model="ir.ui.view"><field name="name">hr.holidays.filter</field><field name="model">hr.holidays</field><field name="arch" type="xml"><search string="Search Leave"><field name="name"/><separator/><filter icon="terp-check" domain="[('state','=','draft')]" string="To Confirm"/><filter icon="terp-camera_test" domain="[('state','in',('confirm','validate1'))]" string="To Approve" name="approve"/><filter icon="terp-camera_test" domain="[('state','=','validate')]" string="Validated" name="validated"/><separator/><filter icon="terp-go-year" name="year" string="Year" domain="[('holiday_status_id.active','=',True)]" help="Filters only on allocations and requests that belong to an holiday type that is 'active' (active field is True)"/><separator/><filter string="My Leaves" icon="terp-personal" name="my_leaves" domain="[('employee_id.user_id','=', uid)]" help="My Leaves"/><separator/><filter string="My Department Leaves" icon="terp-personal+" help="My Department Leaves" domain="[('department_id.manager_id','=',uid)]"/><field name="employee_id"/><field name="department_id"/><field name="holiday_status_id"/><group expand="0" string="Group By..."><filter name="group_name" string="Description" domain="[]" context="{'group_by':'name'}"/><filter name="group_date_from" string="Start Date" icon="terp-personal" domain="[]" context="{'group_by':'date_from'}"/><filter name="group_employee" string="Employee" icon="terp-personal" domain="[]" context="{'group_by':'employee_id'}"/><filter name="group_category" string="Category" icon="terp-stock_symbol-selection" domain="[]" context="{'group_by':'category_id'}"/><filter string="Manager" icon="terp-personal" domain="[]" context="{'group_by':'manager_id'}"/><filter string="Department" icon="terp-personal+" domain="[]" context="{'group_by':'department_id','set_visible':True}"/><filter name="group_type" string="Type" icon="terp-stock_symbol-selection" domain="[]" context="{'group_by':'holiday_status_id'}"/><filter string="Status" icon="terp-stock_effects-object-colorize" domain="[]" context="{'group_by':'state'}"/></group></search></field></record>

View Code

2.在Action中定义,context用于传递创建对象的默认值:

示例代码:XML中定义Action,context用于传递创建对象的默认值

        <act_windowid="act_crm_opportunity_crm_phonecall_new"name="Phone calls"groups="base.group_sale_salesman"res_model="crm.phonecall"view_mode="tree,calendar,form"context="{'default_duration': 1.0 ,'default_opportunity_id': active_id}"view_type="form"/>

View Code

示例代码:Python后台代码中动态给Action的context赋值,context用于传递创建对象的默认值:

    def action_makeMeeting(self, cr, uid, ids, context=None):"""Open meeting's calendar view to schedule meeting on current opportunity.:return dict: dictionary value for created Meeting view"""opportunity = self.browse(cr, uid, ids[0], context)res = self.pool.get('ir.actions.act_window').for_xml_id(cr, uid, 'base_calendar', 'action_crm_meeting', context)res['context'] = {'default_opportunity_id': opportunity.id,'default_partner_id': opportunity.partner_id and opportunity.partner_id.id or False,'default_partner_ids' : opportunity.partner_id and [opportunity.partner_id.id] or False,'default_user_id': uid,'default_section_id': opportunity.section_id and opportunity.section_id.id or False,'default_email_from': opportunity.email_from,'default_name': opportunity.name,}return res

View Code

其中:
context="{'default_duration': 1.0 ,'default_opportunity_id': active_id}"

res['context'] = {
  'default_opportunity_id': opportunity.id,
  'default_partner_id': opportunity.partner_id and opportunity.partner_id.id or False,
  'default_partner_ids' : opportunity.partner_id and [opportunity.partner_id.id] or False,
  'default_user_id': uid,
  'default_section_id': opportunity.section_id and opportunity.section_id.id or False,
  'default_email_from': opportunity.email_from,
  'default_name': opportunity.name,
}
定义了Action对应的对象创建新记录时的默认值。

3.在搜索视图中定义,context用于传递在视图中使用的变量:

示例代码:

        <!-- CRM Lead Search View --><record id="view_crm_case_leads_filter" model="ir.ui.view"><field name="name">CRM - Leads Search</field><field name="model">crm.lead</field><field name="arch" type="xml"><search string="Search Leads"><field name="name" string="Lead / Customer" filter_domain="['|','|',('partner_name','ilike',self),('email_from','ilike',self),('name','ilike',self)]"/><field name="categ_ids" string="Category" filter_domain="[('categ_ids','ilike',self)]"/><field name="section_id" context="{'invisible_section': False, 'default_section_id': self}"/><field name="user_id"/><field name="partner_id" filter_domain="[('partner_id','child_of',self)]"/><field name="create_date"/><field name="country_id" context="{'invisible_country': False}"/><separator/><filter string="Open" name="open" domain="[('state','!=','cancel')]" help="Open Leads"/><filter string="Dead" name="dead" domain="[('state','=','cancel')]"/><filter string="Unassigned" domain="[('user_id','=', False)]" help="No salesperson"/><filter string="Unread Messages" name="message_unread" domain="[('message_unread','=',True)]" help="Unread messages"/><filter string="Assigned to Me"domain="[('user_id','=',uid)]" context="{'invisible_section': False}"help="Leads that are assigned to me"/><filter string="Assigned to My Team(s)"domain="[('section_id.member_ids', 'in', [uid])]" context="{'invisible_section': False}"help="Leads that are assigned to any sales teams I am member of"/><separator /><filter string="Available for mass mailing"name='not_opt_out' domain="[('opt_out', '=', False)]"help="Leads that did not ask not to be included in mass mailing campaigns"/><separator /><group expand="0" string="Group By..."><filter string="Salesperson" domain="[]" context="{'group_by':'user_id'}"/><filter string="Team" domain="[]" context="{'group_by':'section_id'}"/><filter string="Stage" domain="[]" context="{'group_by':'stage_id'}"/><filter string="Customer" help="Partner" domain="[]" context="{'group_by':'partner_id'}"/><filter string="Country" domain="[]" context="{'group_by':'country_id'}"/><filter string="Referrer" domain="[]" context="{'group_by':'referred'}"/><filter string="Campaign" domain="[]" context="{'group_by':'type_id'}"/><filter string="Channel" domain="[]" context="{'group_by':'channel_id'}"/><filter string="Creation" domain="[]" context="{'group_by':'create_date'}"/></group><group string="Display"><filter string="Show Countries" context="{'invisible_country': False}" help="Show Countries"/><filter string="Show Sales Team" context="{'invisible_section': False}" domain="[]" help="Show Sales Team"/></group></search></field></record>

View Code

其中:
<filter string="Show Countries" context="{'invisible_country': False}" help="Show Countries"/> 
定义了在搜索视图中选择“显示国家”时,设置变量 invisible_country 的值为 False,tree 视图中引用变量,设置字段“国家”的显示或隐藏,
tree视图中的代码如下所示:
<field name="country_id" invisible="context.get('invisible_country', True)"/>

4.在Action中定义,context用于传递在后台搜索方法中使用的变量:

示例代码:

        <act_window name="Related Products"context="{'related_product':1}"res_model="product.product"src_model="product.product"view_mode="tree,form"key2="client_action_multi"id="action_related_products2"/>

View Code

其中:
context="{'related_product':1}"
定义了变量 related_product 的值为 1 ,后台代码中引用变量的代码如下所示:

from osv import osv, fieldsclass product_product(osv.osv):_inherit = 'product.product'def search(self, cr, uid, args=None, offset=0, limit=None, order=None,context=None, count=False):if context is None:context = {}if context.get('related_product') and context.get('active_ids'):bom_obj = self.pool.get('mrp.bom')product_bom = bom_obj.search(cr,uid, [('product_id', 'in', context['active_ids'])])parent_bom = [bom.bom_id.id for bom inbom_obj.browse(cr,uid, product_bom) if bom.bom_id.id]rel_ids = [bom.product_id.id for bom in bom_obj.browse(cr, uid, parent_bom)]args.append(('id', 'in', rel_ids))return super(product_product, self).search(cr,uid,args,offset,limit,order,context=context, count=count)return super(product_product, self).search(cr,uid,args,offset,limit,order,context=context, count=count)
product_product()

View Code

后台代码中通过继承并覆写search方法,判断 context 中变量 related_product 的值,共用产品视图中实现了根据产品作为BOM的原料而生成的所有产品的的查询(列表)。

5.在后台代码中传递,context用于方法调用过程中变量的传递:

示例代码:

    #写入,可用于校验写入和更改数据的合法性def write(self, cr, uid, ids, vals, context=None):if context is None:context = {}if not context.get('set_to_draft', False): #非设置为草稿状态的操作for rec in self.browse(cr, uid, ids, context=context):if rec.state == 'draft' and rec.create_uid.id != uid:raise osv.except_osv(_(u'警告!'),_(u'您不能修改他人创建的单据.'))return super(dispatch_sale, self).write(cr, uid, ids, vals, context=context)#设置为草稿状态def set_to_draft(self, cr, uid, ids, context=None):ctx = context.copy()ctx['set_to_draft'] = True #用于在write方法中判断是否是设置为草稿状态的操作return self.write(cr, uid, ids, {'state': 'draft'}, ctx)

View Code

其中:
set_to_draft 方法中定义了变量 ctx['set_to_draft'] = True 并作为参数传递到 write 中,
write 方法中判断变量的值 if not context.get('set_to_draft', False) 执行响应的业务逻辑。

转载于:https://www.cnblogs.com/cnshen/p/3189816.html

openerp学习笔记 context 的应用相关推荐

  1. openerp学习笔记 domain 的应用

    1.在Action中定义,domain用于对象默认的搜索条件: 示例: <record id="action_orders" model="ir.actions.a ...

  2. Golang中WaitGroup、Context、goroutine定时器及超时学习笔记

    原文连接:http://targetliu.com/2017/5/2... 好久没有发过文章了 - -||,今天发一篇 golang 中 goroutine 相关的学习笔记吧,以示例为主. WaitG ...

  3. Json.Net学习笔记

    Json.Net学习笔记 摘自:  http://www.verydemo.com/demo_c360_i45119.html 分类: 编程语言/ ASP.NET/ 文章 导读:string goog ...

  4. Java程序猿的JavaScript学习笔记(12——jQuery-扩展选择器)

    计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...

  5. java/android 设计模式学习笔记(1)--- 单例模式

    前段时间公司一些同事在讨论单例模式(我是最渣的一个,都插不上嘴 T__T ),这个模式使用的频率很高,也可能是很多人最熟悉的设计模式,当然单例模式也算是最简单的设计模式之一吧,简单归简单,但是在实际使 ...

  6. ASP.NET Core 2 学习笔记(七)路由

    ASP.NET Core通过路由(Routing)设定,将定义的URL规则找到相对应行为:当使用者Request的URL满足特定规则条件时,则自动对应到相符合的行为处理.从ASP.NET就已经存在的架 ...

  7. Spring MVC 学习笔记 对locale和theme的支持

    Spring MVC 学习笔记 对locale和theme的支持 Locale Spring MVC缺省使用AcceptHeaderLocaleResolver来根据request header中的 ...

  8. SilverLight学习笔记--Silverlight中WebRequest通讯

    本文我们学习如何使用WebRequest类实现客户端和服务器端的通讯.      本例处理过程:在客户端,我们在文本框中输入任意文本,然后用POST方法向服务器端传递信息,服务器端收到从客户端传来的信 ...

  9. 《繁凡的深度学习笔记》前言、目录大纲 一文让你完全弄懂深度学习所有基础(DL笔记整理系列)

    <繁凡的深度学习笔记>前言.目录大纲 (DL笔记整理系列) 一文弄懂深度学习所有基础 ! 3043331995@qq.com https://fanfansann.blog.csdn.ne ...

最新文章

  1. 分布式文件系统HDFS 练习
  2. 如何使用XML作为小型数据库
  3. js date 加一天_开发效率创新高,只因收下了这波 JS 操作
  4. testid oracle vue,Vue 组件单元测试究竟测试什么?
  5. Windows系统下搭建Git本地代码库
  6. 微信公众号关注用户的信息拉取
  7. dnf全部使用_DNF:1.13拍卖最后1天物价,花瓣礼箱破千万,果然人人都是黑商
  8. window的bat批处理命令
  9. Udacity DNN
  10. Python单词识别
  11. 社科院与杜兰大学金融管理硕士项目—努力的你,值得拥有更好的人生
  12. java web 题_javaWeb习题与答案
  13. 推荐一款绘图神器visual-paradigm
  14. bind dns mysql,linux下bind9.8+dlz+mysql 的dns服务器局域网配置
  15. web实训——3.12
  16. 一份简明的 Markdown 笔记与教程
  17. 感情能慢慢磨合慢慢培养吗_电视慢慢学习如何在网络上玩得开心
  18. 英语语法:词法之非谓语动词--起式
  19. 51单片机使用 8线/4线/PCF8574(I2C) 驱动 LCD1602
  20. Influx 产品常见问题及使用技巧(3)

热门文章

  1. 分区供水条件口诀_经典口诀2020年一建市政管道篇
  2. linux对称哈希 路由,基于 IP 哈希的路由
  3. android auto错位,recyclerview 的item布局错乱
  4. 编辑流程图_如何使用ProcessOn快速绘制一张高颜值流程图?
  5. 请简单叙述微型计算机的诞生,2012湖南省计算机等级考试试题 二级C试题考资料...
  6. mysql2008安装中文_Microsoft SQL Server 2008 R2 中文安装说明
  7. Hadoop核心组件以及发行版
  8. 实战:Python 轻松实现自动化谷歌翻译!
  9. 30 本 Python 新书《谁说菜鸟不会数据分析》,包邮送到你手上!!
  10. 数据分析师最终都会代码化的3点原因?!