OpenERP 在context中写自己的部门ID
使用OpenERP自定义模块开发的时候,你会发现,有一个uid(当前登录用户id)特别好用,不管是在xml的domain
条件表达式中,还是在类中,都能很方便的使用uid.有一段时间就一直在琢磨,这个uid 是什么时候赋值的。感觉是在
session中,一直没有找到。后来需要获取当前登录人的部门id,把department_id做成类似uid这么的变量,就爽了,就
太方便了。
注:Search 资料显示 ['&',('department', '=', user.context_department_id.id),('state', '=', 'pr_draft')]
提示context_department_id不存在,其实这是OE7.0之前的用法,6.0中有类似的方法定义:
- def get_users_department(self, val):
- dep = val.get('department')
- fields = ['name']
- data = [dep]
- if not dep:
- return False
- return self.import_object(fields, data, 'hr.department', 'hr_department_user', dep)
- def get_user_mapping(self):
- return {
- 'model' : 'res.users',
- 'hook' : self.import_user,
- 'map' : {
- 'name': concat('first_name', 'last_name'),
- 'login': value('user_name', fallback='last_name'),
- 'context_lang' : 'context_lang',
- 'password' : 'password',
- '.id' : '.id',
- 'context_department_id/id': self.get_users_department,
- 'user_email' : 'email1',
- }
- }
其实也不是那么神秘,当你实现了下面的步骤:
1) 在context中写入自己的东西(测试)
1.1) 定义一个扩展类,是函数字段
- class extend_user(osv.osv):
- _name = "res.users"
- _inherit = ['res.users']
- # 返回我的id
- def _write_mine_id(self, cr, uid, ids, name, args, context=None):
- result = dict.fromkeys(ids, 1024)
- return result
- _columns = {
- 'context_mine_id': fields.function( _write_mine_id,type="integer"),
- }
- extend_user()
1.2) 在Firefox的Debug下,看请求,奇迹发生了
说明: 不是当时的截图,应该是{"mine_id:": 1024}
1.3) 原理剖析
抽时间在做梳理了
2) 在context中写入当前登录人department_id
2.1) 修改上面的类文
- class extend_user(osv.osv):
- _name = "res.users"
- _inherit = ['res.users']
- # 获取用户关联的部门
- def _get_self_department_ids(self, cr, uid, ids, name, args, context=None):
- result = dict.fromkeys(ids, False)
- emp = self.pool.get('hr.employee')
- for id in ids:
- emp_ids = emp.search(cr, uid, [('user_id', '=', id)], context=context)
- emp = emp.browse(cr, uid, emp_ids[0], context=context)
- result[id] = emp.department_id
- return result
- _columns = {
- 'context_department_id': fields.function(_get_self_department_ids,type="hr.department"),
- }
- extend_user()
2.2) 结果看上面的截图,出来了
2.3) 注意函数字段的类型是 type="hr.department",你没看错就是这个
2.4) 获取部门id另一种方法,职员同名存在问题
- def get_current_user_department_id(self, cr, uid, ids, context=None):
- list_resource_id = self.pool.get('resource.resource').search(cr, uid, [('user_id','=',uid)], context=context)
- username = self.pool.get('resource.resource').browse(cr, uid, list_resource_id[0], context=context).name
- list_emp_id = self.pool.get('hr.employee').search(cr, uid, [('name_related','=',username)], context=context)
- depart_id = self.pool.get('hr.employee').browse(cr, uid, list_emp_id[0], context=context).department_id
- return depart_id
注意:这个return 的depart_id 可不是integer类型,raise Exception(type(depart_id)) :
Exception: <class 'openerp.osv.orm.browse_record'>
raise osv.except_osv(_("test"), _(depart_id))
OpenERP 在context中写自己的部门ID的更多相关文章
- 关于如何在其他包中写controller和简单介绍@SpringBootApplication
本文参考博客:https://blog.csdn.net/u013473691/article/details/52353923 关于@Configuration和@Bean参考博客:https:// ...
- 在mybatis中写sql语句的一些体会
本文会使用一个案例,就mybatis的一些基础语法进行讲解.案例中使用到的数据库表和对象如下: article表:这个表存放的是文章的基础信息 -- ------------------------- ...
- 一种获取context中keys和values的高效方法 | golang
我们知道,在 golang 中的 context 是一个非常重要的包,保存了代码活动的上下文.我们经常使用 WithValue() 这个方法,来往 context 中 传递一些 key value 数 ...
- x01.os.12: 在 windows 中写 OS
在 windows 中写操作系统,需要一系列的辅助工具.在此,要感谢川谷秀实!所有工具,都在 z_tools 文件夹中.有了大师的帮助,不妨也来尝试在 windows 中写一把 OS. 源代码及工具可 ...
- oracle调用java方法的例子(下面所有代码都是在sql/plus中写)
在Oracle中调用Java程序,注意:java方法必须是static类型的,如果想在JAVA中使用system.out/err输出log. 需要在oracle 中执行"call dbms_ ...
- 【Filter 不登陆无法访问】web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面的功能
在web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面,而重定向到登陆界面的功能. 项目是用springMVC+spring+hibernate实现 (和这个没有多大关系) 第一步: 首先 ...
- python中写shell(转)
python中写shell,亲测可用,转自stackoverflow To run a bash script, copy from stackoverflow def run_script(scri ...
- css笔记——关于css中写上charset “utf-8”
当css文件中写上 charset "utf-8" 时需要将body和html的样式分开写 例如: html,body{margin:0;padding:0;font-family ...
- 用RelativeLayout布局可以在imageview中写上文字
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
随机推荐
- [重要更新][Quartus II][14.1正式版]
[Quartus II][14.1正式版] ----14.1版本最大的变化就是增加了2大系列的器件库: MAX 10和Arria 10.这2大系列据Altera中国区代理 骏龙科技的人说,就是为了和X ...
- java的(PO,VO,TO,BO,DAO,POJO)解释1
java的(PO,VO,TO,BO,DAO,POJO)解释 O/R Mapping 是 Object Relational Mapping(对象关系映射)的缩写.通俗点讲,就是将对象与关系数据库绑定 ...
- GDB高级用法
http://blog.csdn.net/wwwsq/article/details/7086151
- HDU 2686 Matrix(最大费用流)
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- Windows 7 卸载 IE10
今天微软为Windows 7发布了IE10预览版,你是否已经安装?根据笔者的体验,IE10确实如微软所说,在速度.性能等各方面都有了明显提升. 不过,IE10发布预览版安装后会直接替代IE9,如果你想 ...
- Oracle rac架构和原理
Oracle RAC Oracle Real Application Cluster (RAC,实时应用集群)用来在集群环境下实现多机共享数据库,以保证应用的高可用性:同时可以自动实现并行处理 ...
- AlertView动画
AlertView动画 效果 源码 https://github.com/YouXianMing/Animations // // AbstractAlertView.h // Animations ...
- 《马上有招儿:PPT商务演示精选20讲(全彩) 》
<马上有招儿:PPT商务演示精选20讲(全彩) > 基本信息 作者:马建强 霍然 出版社:电子工业出版社 ISBN:9787121225123 上架时间:2014-3-11 出版日期 ...
- TreeMap源代码深入剖析
第1部分 TreeMap介绍 A Red-Black tree based NavigableMap implementation. The map is sorted according to th ...
- C语言:结构体和联合体(共用体)
结构体:struct 1.结构体变量的首地址能够被其最宽基本类型成员的大小所整除. 2.结构体每个成员相对于结构体首地址的偏移量(offset)都是成员的整数倍. 3.结构体的总大小为结构体最宽基本类 ...