0

I have written a method _update_config_list which should automatically be called when a record has been created in account.analytic.line. This method should create a new record in the One2Many field if the Employee is not found in the One2Many field automatically.
But my code doesn't do anything.

from datetime import timedelta

from odoo import api, fields, models, tools
from odoo.exceptions import UserError, AccessError, ValidationError


class RoleSync(models.Model):

    _inherit = 'account.analytic.line'

    role_field_id = fields.Many2one(
        string='Role', compute='_compute_role', comodel_name='project.roles')
    # role_addline = fields.Many2one('project.roles', compute="role_addline")
    remaining_time_id = fields.Float(related='task_id.remaining_hours', readonly=True,
                                     string="Time remaining  ")

    def _update_config_list(self):
        list_id = []
        for rec in self:
            if rec.project_id:
                list = self.env['project.project'].search(
                    [('name', '=', rec.project_id.name)])
                for val in list.list_id:
                    if rec.employee_id != val.emp_id:
                        list_id.append(rec.employee_id)
                list_id = set(list_id)
                list.update({'list_id': list_id})

    @api.model
    def create(self, values):
        result = super(RoleSync, self).create(values)
        self._update_config_list()
        return result

This is my class for the list_id:

from datetime import timedelta, datetime
from calendar import monthrange


from odoo import api, fields, models, tools
from odoo.exceptions import UserError, AccessError, ValidationError
from odoo.tools import date_utils


#-----------------------------------------------------------------------------------------------#


class ProjectClass(models.Model):
    _inherit = 'project.project'

    list_id = fields.One2many(
        'roles.roles', 'project_id', string="Config_List")

class RolesClass(models.Model):
    _name = 'roles.roles'

    emp_id = fields.Many2one('hr.employee', string="Employee")
    role_id = fields.Many2one('project.roles', string="Role")
    project_id = fields.Many2one(
        'project.project', string="Project", readonly='1')

If a new record was created in this environment It should automatically be added in this One2Many field

Eric Jin
  • 3,836
  • 4
  • 19
  • 45
for-loop
  • 47
  • 1
  • 8
  • It's difficult to understand your code without knowing what `list_id` is or will be. Please add the code (partly) about that stuff. And then there is a little mistake in the `create()`: you're calling `_update_config_list()` on `self`. This methods iterates on records, but `self` in this context (creation) is an empty recordset. You should call it on `result` instead, which is the recordsset of the created records. – CZoellner Oct 05 '21 at 09:57
  • Yes I forgot to include my code for the One2Many field which is my List. It has 3 fields, one is emp_id and I have to create a new entry automatically . – for-loop Oct 05 '21 at 10:54

2 Answers2

1

You can use Odoo's X2Many notation: https://www.odoo.com/documentation/14.0/developer/reference/addons/orm.html#odoo.models.Model.write

In your case it would be either 0 (create) or 4 (link):

rec.write({'list_id': [(0, 0, {values})]})

# OR

rec.write({'list_id': [(4, id)]})
adekock11
  • 544
  • 2
  • 11
0

You can use like list.list_id = [(6, 0, list_id)] it replace the all records and list_id records more notation you can find on. https://www.odoo.com/documentation/14.0/developer/reference/addons/orm.html#odoo.models.Model.write

    def _update_config_list(self):
        list_id = []
        for rec in self:
            if rec.project_id:
                list = self.env['project.project'].search(
                    [('name', '=', rec.project_id.name)])
                for val in list.list_id:
                    if rec.employee_id != val.emp_id:
                        list_id.append(rec.employee_id)
                c = set(list_id)
                list.list_id = [(6, 0, list_id)]