使用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中有类似的方法定义:

  1. def get_users_department(self, val):
  2. dep = val.get('department')
  3. fields = ['name']
  4. data = [dep]
  5. if not dep:
  6. return False
  7. return self.import_object(fields, data, 'hr.department', 'hr_department_user', dep)
  8. def get_user_mapping(self):
  9. return {
  10. 'model' : 'res.users',
  11. 'hook' : self.import_user,
  12. 'map' : {
  13. 'name': concat('first_name', 'last_name'),
  14. 'login': value('user_name', fallback='last_name'),
  15. 'context_lang' : 'context_lang',
  16. 'password' : 'password',
  17. '.id' : '.id',
  18. 'context_department_id/id': self.get_users_department,
  19. 'user_email' : 'email1',
  20. }
  21. }

其实也不是那么神秘,当你实现了下面的步骤:

1)  在context中写入自己的东西(测试)

1.1) 定义一个扩展类,是函数字段

  1. class extend_user(osv.osv):
  2. _name = "res.users"
  3. _inherit = ['res.users']
  4. # 返回我的id
  5. def _write_mine_id(self, cr, uid, ids, name, args, context=None):
  6. result =  dict.fromkeys(ids, 1024)
  7. return result
  8. _columns = {
  9. 'context_mine_id': fields.function( _write_mine_id,type="integer"),
  10. }
  11. extend_user()

1.2)  在Firefox的Debug下,看请求,奇迹发生了

说明:  不是当时的截图,应该是{"mine_id:": 1024}

1.3)  原理剖析

抽时间在做梳理了

2)  在context中写入当前登录人department_id

2.1)  修改上面的类文

  1. class extend_user(osv.osv):
  2. _name = "res.users"
  3. _inherit = ['res.users']
  4. # 获取用户关联的部门
  5. def _get_self_department_ids(self, cr, uid, ids, name, args, context=None):
  6. result = dict.fromkeys(ids, False)
  7. emp = self.pool.get('hr.employee')
  8. for id in ids:
  9. emp_ids = emp.search(cr, uid, [('user_id', '=', id)], context=context)
  10. emp = emp.browse(cr, uid, emp_ids[0], context=context)
  11. result[id] = emp.department_id
  12. return result
  13. _columns = {
  14. 'context_department_id': fields.function(_get_self_department_ids,type="hr.department"),
  15. }
  16. extend_user()

2.2)  结果看上面的截图,出来了

2.3)  注意函数字段的类型是  type="hr.department",你没看错就是这个

2.4) 获取部门id另一种方法,职员同名存在问题

  1. def get_current_user_department_id(self, cr, uid, ids, context=None):
  2. list_resource_id = self.pool.get('resource.resource').search(cr, uid, [('user_id','=',uid)], context=context)
  3. username = self.pool.get('resource.resource').browse(cr, uid, list_resource_id[0], context=context).name
  4. list_emp_id = self.pool.get('hr.employee').search(cr, uid, [('name_related','=',username)], context=context)
  5. depart_id = self.pool.get('hr.employee').browse(cr, uid, list_emp_id[0], context=context).department_id
  6. 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))

最新文章

  1. 012.对netmap API的解读
  2. spring源码分析之spring jmx
  3. MYSQL 的错误Incorrect information in file: &#39;.\test\stuff.frm
  4. linux第6天 流协议-粘包
  5. ERP_Oracle Erp发展趋势基于SOA电子商务方案
  6. MVC 初始 Log4net (一)
  7. C# winform 选择项 省市连动
  8. [Unity-24] Unity的四种载入场景的方法
  9. Nginx+Tomcat+Redis实现持久会话
  10. foo的出现
  11. Linux Shell 命令--awk
  12. Promise的实现原理
  13. 第五章703N 刷openwrt 挂载u盘
  14. 【emWin】例程二十四:窗口对象——Header
  15. java基本数据类型的范围
  16. MVC 设计模式概述
  17. ngingx安装错误 ./configure: error: the HTTP rewrite module requires the PCRE library.
  18. 删除RAC集群节点
  19. C Language Study - 函数指针的使用
  20. nSum “已知target再求和”类型题目总结:n-2重循环+left/right

热门文章

  1. Javascript 面向对象编程(一):封装(转载)
  2. JSONObject以及json(转)
  3. 【spring boot】【log4jdbc】使用log4jdbc打印mybatis的sql和Jpa的sql语句运行情况
  4. Design Pattern Singleton 单一模式
  5. 重载 UINavigationController 设置左侧返回按钮的文字为图片
  6. java自动创建多级目录
  7. jaxb使用
  8. opencv编译Python接口
  9. C错误异常处理,异常处理
  10. 学习笔记:A*算法