6

I have the following function in the class hr_evaluation_interview:

@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'm just giving id=119 in the query 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 field deadline when the value of field evaluation_id changes. Again for just testing.

What I really need is to execute a query similar to what I mentioned. However when I execute this query nothing is printing on 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 checking online why this warning, but got no help. Am I doing something wrong? How exactly can I execute query from within @api.onchange(self)?

2
  • There are many such examples already available in core code of odoo for executing the query. Here after executing the query, you still need to call the fetch statement, to get the result. Commented Sep 15, 2015 at 10:52
  • could you please provide some code? Commented Sep 15, 2015 at 11:38

2 Answers 2

5

As Hardik said, cr.execute() doesn't return directly you result. You need to fetch the values from the cursor after executing the query. Try 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]
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer but it says AttributeError: 'NoneType' object has no attribute 'fetchone' :/ Any idea what's wrong?
I tried this query with Postgres separately and it works fine so the return is not None for this query.
The database I'm trying to fetch data from is defined in another model class (hr_evaluation_interview) and I'm working in another model class in the same file (hr_evaluation_interview). But when executing the query, it doesn't matter from which model I'm executing right?
1

If evaluation_id is m2o field of hr.evaluation.evaluation model you can try below code. you don't need to execute query 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.