如何从视图中获取过滤数据?

时间:2016-04-27 16:26:56

标签: python xml openerp

我添加了一个模块" Base Candidat"在这个模块base.candidat我想打开一个特定候选人的应用程序(与候选人有相同的电子邮件)我将此功能添加到base_candidat.py

class base_candidat(osv.osv):
    _columns = { 
        'candidat_name': fields.char('Candidat', size=128, required=True),
        'blacklist': fields.boolean('Blacklist'),
        'email_candidat': fields.char('Email', size=32),
        'mobile': fields.char('Mobile', size=32),
        'priority': fields.selection(AVAILABLE_PRIORITIES, 'Appreciation'),
        'user_id': fields.many2one('res.users', 'Responsable'),
        'specialite': fields.many2one('base.candidat.specialite', 'Spécialité'),
    }

    def action_get_applications(self, cr, uid, ids, context=None):
        modelDataClass = self.pool.get('ir.model.data')
        hrApplicantClass = self.pool.get('hr.applicant')

        model, action_id = modelDataClass.get_object_reference(cr, uid, 'hr_recruitment', 'action_hr_job_applications')
        action = self.pool.get(model).read(cr, uid, action_id, context=context)
        email_ids = self.browse(cr, uid, ids[0], context).email_candidat
        candidature_ids = hrApplicantClass.search(cr, uid, [('email_from', '=', email_ids)], context=context)

        action['context'] = {'default_res_model': self._name, 'default_res_id': ids[0]}
        action['domain'] = str([('candidature_ids', 'in', email_ids)])

        return action

我没有结果。有什么建议??

1 个答案:

答案 0 :(得分:0)

如果我知道,'action'是一个不允许的变量名称。如果使用它,程序将不一致。 (与'主动'相同的情况)

在我看来,这个域名表达在这一行中是不正确的:
action['domain'] = str([('candidature_ids', 'in', email_ids)])
因为这意味着你的模型有candidature_ids字段,但模型没有,所以这个条件不能满足,所以当你使用它时你看不到任何记录。
你的概念的另一个问题,email_ids是字符串(不是数字,因此变量名称具有欺骗性),candidature_ids是数字列表。

在我看来,你想写一个函数字段。这是一个示例代码,它可能不是最佳性能,但我认为它将帮助您理解功能域的本质:

def _getApplications(self, cr, uid, ids):
    hrApplicantClass = self.pool.get('hr.applicant')

    # get all of candidate
    candidateDatas = self.read(cr, uid, ids, ['email_candidat'])

    # collect all of emails
    emails = []
    for candidate in candidateDatas:
        if candidate['email_candidat'] not in emails:
            emails.append(candidate['email_candidat'])

    # get all of relevant applicant
    applicantIds = hrApplicantClass.search(cr, uid, [('email_from', 'in', emails)])
    applicantDatas = hrApplicantClass.read(cr, uid, applicantIds, ['email_from'])

    # pair base_candidat id with application id
    retVal = {}
    for candidate in candidateDatas:
        retVal[candidate['id']] = []
        if not candidate['email_candidat']:
            continue
        else:
            for applicant in applicantDatas:
                if applicant['email_from'] == candidate['email_candidat']:
                    retVal[candidate['id']].append(applicant['id'])

    return retVal

# the field definition
'application_ids': fields.function(_getApplications, method=True, type='one2many', 'Applications')