Odoo 10 错误 - 我需要将 one2many 值发送​​到另一个具有相同 one2many 字段的模型

Odoo 10 Error - I need to send one2many values to another model has the same one2many field

我收到 Odoo 10 错误。

我需要将 one2many 值发送​​到另一个具有相同 one2many 字段的模型。

当我使用我的工作流程发送 one2many 字段时,我遇到了这个错误预期单例,(1,4,6)。

我该如何解决这个问题?

file_upload_material = fields.One2many('file.decision','param_two')

@api.multi
def confirm(self):
    file = self.file_upload_material.file
    name = self.file_upload_material.name
    if self.name:
        media_and_media = {
            'name': self.name,
            'instructions': self.instructions,
            'task_id': self.task_id.id,
            'descriptions': self.descriptions,
            'date_from_exec': self.date_from_exec,
            'date_to_exec': self.date_to_exec,
            'date_from': self.date_from,
            'task_name': self.task_name,
            'file_upload_material': [(0, 0, {
                'name': name,
                'file': file
            })],

        }
        self.env['my.task.log'].create(media_and_media)
        self.state = 'unread'


        class LogMydecision(models.Model):
             _name = "my.task.log"
       file_upload_material = fields.One2many('file.decision','param_one')


       class FileUpload(models.Model):

            _name = 'file.decision'

            name = fields.Char('File Name')
            file = fields.Binary('Upload File')
            param_one = fields.Many2one('my.task.log)
            param_two = fields.Many2one('task.log)

您的错误似乎与包含记录集的字段 file_upload_material 的值有关,您使用它就像只包含一个记录(如 m2o)一样,特别是在这一行:

file = self.file_upload_material.file
name = self.file_upload_material.name

您的问题可以这样解决:

media_and_media = {
    'name': self.name,
    'instructions': self.instructions,
    'task_id': self.task_id.id,
    'descriptions': self.descriptions,
    'date_from_exec': self.date_from_exec,
    'date_to_exec': self.date_to_exec,
    'date_from': self.date_from,
    'task_name': self.task_name,
    'file_upload_material': [(0, 0, {
        'name': fum.name,
        'file': fum.file
    }) for fum in self.file_upload_material],
}

似乎您正在通过在确认方法中复制来复制 file.decision 记录的值,您可以将该字段更改为 m2m 并重新使用它,或者您也可以编写现有记录之间的关系使用 (4, id) 或 (6,0,[ids]) 而不是使用 (0,0,{vals}),像这样:

media_and_media = {
    'name': self.name,
    'instructions': self.instructions,
    'task_id': self.task_id.id,
    'descriptions': self.descriptions,
    'date_from_exec': self.date_from_exec,
    'date_to_exec': self.date_to_exec,
    'date_from': self.date_from,
    'task_name': self.task_name,
    'file_upload_material': [(6, 0, self.file_upload_material.ids)],
}

即防止记录重复。此外,我会建议您切换到 file_upload_material 关系

的 m2m 字段