2017-07-19 62 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), 
    } 

還需要在依賴列表中添加郵件模塊。