2

Getting above error creating new record in one2many field.

code is below:

def _compute_timesheet_hour(self):
    for val in self:
        self._cr.execute('''SELECT project_id, employee_id, SUM(unit_amount) FROM account_analytic_line 
        where project_id = %(project_id)s and employee_id = %(employee_id)s
        GROUP BY project_id, employee_id''', { 'project_id': val.project_id.id, 'employee_id': val.employee_id.id,})
        res = self._cr.fetchone()
        if res and res[2]:
            val.timesheet_hour = res[2]
        else:
            val.timesheet_hour = 0.0
Pawan Kumar Sharma
  • 1,168
  • 8
  • 30
  • This is going to need more information. What is `self` in this context? How are `project.id` and `employee_id` defined? – Adrian Klaver Dec 18 '20 at 18:18

2 Answers2

0

At create moment the value of record.id doesn't exist in database. When creating a record for a model with computed fields, the records of the record-set will be in memory only. At that time the id of the record will be dummy ids of type odoo.models.NewId.

Ibrahim Rahimi
  • 519
  • 8
  • 31
0
def _compute_timesheet_hour(self):
    for val in self:
        users = self.env['account.analytic.line'].search([('project_id', '=', val.project_id.id), ('employee_id', '=', val.employee_id.id)])
        if users:
            unit_amount = users.mapped('unit_amount') or [0.0]
            val.timesheet_hour = sum(unit_amount)
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 06 '23 at 15:10