Odoo Help

Welcome!

This community 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.

0

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

By
Yassine TEIMI
on 11/24/15, 3:04 PM 558 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

--Axel Mendoza--
10205
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

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