2017-07-19 53 views

回答

1

message_follower_ids是计算领域。

如果你想通过任何计算领域进行搜索,你必须在旧的api中编写它的搜索方法,它是fnct_search,并且在那个方法中你可以返回域。

在你的情况下,message_follower_ids是计算一个,也有fnct_search方法。所以,无论何时您搜索右上角搜索栏中的关注者,该方法都会调用并返回域,您将获得过滤列表。

但在该fnct_search您需要改变以完成您的需要。

像这样。

class mail_thread(osv.AbstractModel): 
    _inherit = 'mail.thread'  

    def _get_followers(self, cr, uid, ids, name, arg, context=None): 
     fol_obj = self.pool.get('mail.followers') 
     fol_ids = fol_obj.search(cr, SUPERUSER_ID, [('res_model', '=', self._name), ('res_id', 'in', ids)]) 
     res = dict((id, dict(message_follower_ids=[], message_is_follower=False)) for id in ids) 
     user_pid = self.pool.get('res.users').read(cr, uid, [uid], ['partner_id'], context=context)[0]['partner_id'][0] 
     for fol in fol_obj.browse(cr, SUPERUSER_ID, fol_ids): 
      res[fol.res_id]['message_follower_ids'].append(fol.partner_id.id) 
      if fol.partner_id.id == user_pid: 
       res[fol.res_id]['message_is_follower'] = True 
     return res 

    def _search_followers(self, cr, uid, obj, name, args, context): 
     """Search function for message_follower_ids 

     Do not use with operator 'not in'. Use instead message_is_followers 
     """ 
     fol_obj = self.pool.get('mail.followers') 
     res = [] 
     for field, operator, value in args: 
      assert field == name 
      # TOFIX make it work with not in 
      assert operator != "not in", "Do not search message_follower_ids with 'not in'" 
      fol_ids = fol_obj.search(cr, SUPERUSER_ID, [('res_model', '=', self._name), ('partner_id', operator, value)]) 
      if not fol_ids and operator == '=' and value==False: 
       fol_ids = fol_obj.search(cr, SUPERUSER_ID, [('res_model', '=', self._name), ('partner_id', '!=', value)]) 
       res_ids = [fol.res_id for fol in fol_obj.browse(cr, SUPERUSER_ID, fol_ids)] 
       res.append(('id', 'not in', res_ids)) 
      else: 
       res_ids = [fol.res_id for fol in fol_obj.browse(cr, SUPERUSER_ID, fol_ids)] 
       res.append(('id', 'in', res_ids)) 
     return res 

    _columns = { 
     'message_follower_ids': fields.function(_get_followers,fnct_search=_search_followers), 
    } 

还需要在依赖列表中添加邮件模块。

相关问题