How to execute a request in Odoo-8 from Python?

I have the following function in the hr_evaluation_interview class:

 @api.onchange('evaluation_id') def onchange_evalID(self): self.deadline=self.env.cr.execute('SELECT date FROM hr_evaluation_evaluation where id=119') 

Note. I just suggest id=119 in the request for testing purposes.

When I give self.deadline=datetime.now.strftime(%Y-%m-%d %H:%M:%S") , it works fine and changes the value of the deadline field when the value of the evaluation_id field changes. Again for simple testing.

I really need to execute a query similar to what I mentioned. However, when I execute this query, nothing prints in the deadline field. When I check the log, I see this warning:

 WARNING db_name openerp.models: Cannot execute name_search, no _rec_name defined on hr_evaluation.evaluation 

I tried to check online why this warning, but did not get any help. Am I doing something wrong? How to accurately execute a request from @api.onchange(self) ?

+6
source share
2 answers

As Hardik said, cr.execute() does not directly return the result. After executing the query, you need to extract the values ​​from the cursor. Try it like this:

 @api.onchange('evaluation_id') def onchange_evalID(self): self.env.cr.execute('SELECT date ' 'FROM hr_evaluation_evaluation where id=119') self.deadline = self.env.cr.fetchone()[0] 
+5
source

If evaluation_id is the m2o field of the m2o model, you can try running the code below. you don’t have to complete the request at all.

 @api.onchange('evaluation_id') def onchange_evalID(self): if self.evaluation_id and self.evaluation_id.date: self.date = self.evaluation_id.date 
+1
source

All Articles