for-loop
for-loop

Reputation: 57

How to add new record to One2Many field automatically via create function in different model?

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

Upvotes: 0

Views: 2000

Answers (2)

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)]

Upvotes: 0

adekock11
adekock11

Reputation: 614

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)]})

Upvotes: 1

Related Questions