当前位置: 首页 > 知识教程 > 为什么openerp中查不到未激活的用户
为什么openerp中查不到未激活的用户
Odoo,OpenERP中文网 / 2014-05-21

 管理员将一位休长假的员工设置成了未激活状态。现在这名员工上班了,需要恢复成激活状态,但是在用户列表中找不到了。 最后直接在数据库中,更改记录的active字段解决了问题。而这种直接修改底层数据的操作可能是内控程序所不允许的。

 
为什么openerp不显示未激活的员工记录呢?我们知道, openerp是通过search方法来得记录号, 进而用read方法读写这些记录号的信息。 search方法中的domian参数限定了搜索范围, 例如, 当fileter 为['|', ('id', u'>', 1), ('id', u'=', 1)] 时,administrator用户应该能看到所有的用户, 其他人不能看到administrator用户。 现在的情况是, domain中并没有对是否激活做出限制呀。
 
我们只能这样猜想, 也许res.user实现了自己的search方法,我们查看base目录下的users.py, 没有发现。 也许有其他模块继承了res.users, 对serach方法做了覆盖, 也没有找到。
 
那我们只有去看orm中实现的search方法了
def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
     return self._search(cr, user, args, offset=offset, limit=limit,
                         order=order, context=context, count=count)
  
它直接调用了_search
  def _search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False, access_rights_uid=None):
       .....
       query = self._where_calc(cr, user, args, context=context)
       self._apply_ir_rules(cr, user, query, 'read', context=context)
       order_by = self._generate_order_by(order, query)
       from_clause, where_clause, where_clause_params = query.get_sql()
       ....
       cr.execute('SELECT "%s".id FROM ' % 
            self._table + from_clause + where_str + order_by + limit_str + offset_str,
            where_clause_params)
 
我们看到,是_where_calc 生成了查询条件query
 def _where_calc(self, cr, user, domain, active_test=True, context=None):
    ....
    if 'active' in self._all_columns and (active_test and context.get('active_test', True)):
            if domain:
                if not any(item[0] == 'active' for item in domain):
                    domain.insert(0, ('active', '=', 1))
            else:
                domain = [('active', '=', 1)]
   ....
在这个长长的if语句中, 有连串的and条件
--- 本模型中有active字段, res.users符合这个条件, 
--- active_test:_search 调用_where_calc时未设置这个参数, 默认值是True, 这个条件也符合
--- 取上下文'active_test', 因为在用户列表的活动窗口中没有给出这个上下文, context.get指定了True
因此这个长长的if语句的条件是满足的, 结果就是在domain中增加了active=1, (这还有一个限制,domain中没有指定active, 即 if not any(item[0] == 'active' for item in domain)
 
到此为止, 我们找到了原因, 也就能找到了解决问题的方法
-- 修改act-window, 在上下文中指定 "active_test":False 或
-- 在搜索条件中指定active=False  (它只能显示未激活的用户, 并且active可能会被翻译成别的名字)
 
最后要说的是, 除了res.users, 所有带有active字段的, 都有同样的现象。