簡體   English   中英

如何通過在不同的 model 中創建 function 自動將新記錄添加到 One2Many 字段?

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

我寫了一個方法_update_config_list應該在account.analytic.line中創建記錄時自動調用。 如果在 One2Many 字段中未自動找到Employee ,則此方法應在 One2Many 字段中創建一個新記錄。
但是我的代碼沒有做任何事情。

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

這是我的 list_id 的 class:

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

如果在此環境中創建了新記錄 它應該自動添加到這個 One2Many 字段中

您可以使用 Odoo 的 X2Many 表示法: https://www.odoo.com/documentation/14.0/developer/reference/addons/orm.html#odoo.models.Model.write

在您的情況下,它將是 0(創建)或 4(鏈接):

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

# OR

rec.write({'list_id': [(4, id)]})

您可以使用像 list.list_id = [(6, 0, list_id)] 它替換所有記錄和 list_id 記錄更多您可以找到的符號。 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)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM