Odoo Help

0

Modify ORM create method behavior two times using inheritance [Old API] [SOLVED]

By
Yassine TEIMI
on 11/24/15, 3:04 PM 940 views

Dears,

I want to change the following create method behavior (The code part I want to change is in bold) :

class hr_evaluation_interview(osv.Model): 
_name = 'hr.evaluation.interview'
_inherit = 'mail.thread'
_rec_name = 'user_to_review_id'
_description = 'Appraisal Interview'
def create(self, cr, uid, vals, context=None):
    phase_obj = self.pool.get('hr_evaluation.plan.phase')
    survey_id = phase_obj.read(cr, uid, vals.get('phase_id'), fields=['survey_id'], context=context)['survey_id'][0]
    if vals.get('user_id'):
        user_obj = self.pool.get('res.users')
        partner_id = user_obj.read(cr, uid, vals.get('user_id'), fields=['partner_id'], context=context)['partner_id'][0]

    else:
        partner_id = None
    user_input_obj = self.pool.get('survey.user_input')
    if not vals.get('deadline'):
        vals['deadline'] = (datetime.now() + timedelta(days=28)).strftime(DF)
        ret = user_input_obj.create(cr, uid, {'survey_id': survey_id,
            'deadline': vals.get('deadline'),
            'type': 'link',
            'partner_id': partner_id}, context=context)
         vals['request_id'] = ret
    return super(hr_evaluation_interview, self).create(cr, uid, vals, context=context)

It's the hr_evaluation_interview create method, I want when the interview request is created, the partner_id field receive a different value than it receives now.

The partner_id receives the user_id (user related to the hr evaluator), I want it to be the evaluated employee (interviewd_id).

So I did the following using inheritance (changed code is in bold) :

class HrEvaluationInterview(osv.Model): 
_inherit = 'hr.evaluation.interview'
_columns = {
'interviewd_id': fields.many2one('hr.employee','Employee evaluated'),
}
def create(self, cr, uid, vals, context=None):
    phase_obj = self.pool.get('hr_evaluation.plan.phase')
    survey_id = phase_obj.read(cr, uid, vals.get('phase_id'), fields=['survey_id'], context=context)['survey_id'][0]
    employee_obj = self.pool.get('hr.employee')
    us_id = employee_obj.read(cr, uid, vals.get('interviewd_id'), fields=['user_id'], context=context)['user_id'][0]
    if vals.get('user_id'):
        user_obj = self.pool.get('res.users')
        partner_id = user_obj.read(cr, uid, us_id, fields=['partner_id'], context=context)['partner_id'][0]

    else:
        partner_id = None
        user_input_obj = self.pool.get('survey.user_input')
    if not vals.get('deadline'):
        vals['deadline'] = (datetime.now() + timedelta(days=28)).strftime(DF)
    ret = user_input_obj.create(cr, uid, {'survey_id': survey_id,
        'deadline': vals.get('deadline'),
        'type': 'link',
        'partner_id': partner_id}, context=context)
    vals['request_id'] = ret
    return super(HrEvaluationInterview, self).create(cr, uid, vals, context=context)

The problem is that the behavior didn't change, it keeps doing the same things did by the parent method. Maybe it's because the return I'm doing, can you advise me something ?

Thanks a lot.

1
Axel Mendoza
On 11/24/15, 4:57 PM

Hi @Yassine TEIMI

You need to bypass the super method in your return by changing this:

return super(HrEvaluationInterview, self).create(cr, uid, vals, context=context)

to this:

return osv.Model.create(self, cr, uid, vals, context=context)

This have the drawback that it will skip all the next create method overrides that could be executed by the super call but it will work

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

A better way to do the changes that you need is like:

class HrEvaluationInterview(osv.Model): 
_inherit = 'hr.evaluation.interview'
_columns = {
'interviewd_id': fields.many2one('hr.employee','Employee evaluated'),
}

def create(self, cr, uid, vals, context=None): 
us_id = self.pool.get('hr.employee').read(cr, uid, vals.get('interviewd_id'), fields=['user_id'], context=context)['user_id'][0]
vals['user_id'] = us_id
return super(HrEvaluationInterview, self).create(cr, uid, vals, context=context)

The approach is to change the vals['user_id'] so the code of original create method get another partner based on the changed user_id. With your old code you left the user_id in vals intact and that can lead to others errors, but if what you need to do is to have the same user_id but a different partner_id then left that as is. This is just another solution

Indeed that's what I was looking for, the create method first override is now replaced correctly. it creates just one time not twice. Thanks. But, I still have a problem with partner_id field it doesn't get the correct value (the same as old one) I'm looking for why, because the main purpose of this override is to change the partner_id received value.

Yassine TEIMI
on 11/24/15, 5:23 PM

Did you mean to change the partner_id passed to the create of the survey.user_input??

Axel Mendoza
on 11/24/15, 5:32 PM

Yes, I've checked it, it works fine now, partner_id with the correct value. Thanks a lot Axel.

Yassine TEIMI
on 11/24/15, 5:36 PM

Happy to help

Axel Mendoza
on 11/24/15, 5:40 PM

Just a final question, it will work if I use self.create(cr, uid, vals, context) instead of osv.Model.create(self,cr,uid, vals, context) ?

Yassine TEIMI
on 11/24/15, 5:41 PM

I think that it will not work, but you are free to try it

Axel Mendoza
on 11/24/15, 5:45 PM

Okay, Thanks.

Yassine TEIMI
on 11/24/15, 5:46 PM

I used before to call self.create and lead me to very rare behavior like recursive call to the same create method, but if that works for you, go ahead

Axel Mendoza
on 11/24/15, 5:47 PM

see the answer update for another approach

Axel Mendoza
on 11/25/15, 12:54 AM

Yes, that's what i was thinking, the create method recursiveness, because you call the same method, and it gives an infinite calling loop. Thank you for the second appraoch, indeed, it's a better one, I'll test it. Cheers.

Yassine TEIMI
on 11/25/15, 5:57 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 11/24/15, 3:04 PM
Seen: 940 times
Last updated: 11/26/15, 6:37 AM