【问题标题】:odoo validation error when trying to create a stock.move from onchange function尝试从 onchange 函数创建 stock.move 时出现 odoo 验证错误
【发布时间】:2018-07-28 21:16:57
【问题描述】:

所以我想通过@onchange 函数在stock.picking 中自动创建一条移动线(move_lines)。这是我的功能:这只是一个小测试。当字段changed 的值发生变化时,我将其作为移动行中产品的ID (product_id),然后将该移动行附加到现有的move_lines 列表中。

NB1:move_linesstock.picking 中的 One2many 关系。

NB2:product_idstock.move 中的声明:

product_id = fields.Many2one(
        'product.product', 'Product',
        domain=[('type', 'in', ['product', 'consu'])], index=True, required=True,
        states={'done': [('readonly', True)]})

我的功能:

changed = fields.Integer('Changed')

@api.onchange('changed')
    def _changed_onchange(self):
        move_lines = []

        for line in self.move_lines:
            move_lines.append({'product_id': line.product_id.id or False,
                               'product_qty': line.product_qty or 0,
                               'name': line.product_id.name,
                               'product_uom': line.product_uom.id,
                               'date_planned': datetime.date.today(),
                               'date_expected': datetime.date.today()
                               })

        move_lines.append({'product_id': self.changed,
                           'name': 'default',
                           'product_uom': 1,
                           'date_planned': datetime.date.today(),
                           'date_expected': datetime.date.today()
                           })

        return {'value': {'move_lines': move_lines}}

如果我使用视图创建移动线然后保存,一切正常,但是当我更改字段的值以便函数插入新的移动线时,保存不起作用,我一直得到错误:

Odoo Server Error - Validation Error
The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set

[object with reference: product_id - product.id]

有什么问题?

【问题讨论】:

  • 您将 move_lines 作为将被忽略的字典列表传递,它将尝试使用默认值创建移动并导致“未正确设置必填字段”。检查我的答案。

标签: odoo erp odoo-11 odoo-view


【解决方案1】:

要将行追加到move_lines,可以使用以下语法:

@api.onchange('changed')
def _changed_onchange(self):
    values = {'product_id': self.changed,
              'name': 'default',
              'product_uom': 1,
              'date_planned': datetime.date.today(),
              'date_expected': datetime.date.today(),
              'location_id': 1,
              'location_dest_id': 1
              }

    self.move_lines |= self.move_lines.create(values)

要使用上述逻辑实现它,您可以尝试x2many values filling,但我建议您使用Set operations,您可以在Lunching wizards 找到示例

【讨论】:

  • 但这是添加的新元素。 self.changed 是它的 id。我的意思是 for 循环遍历现有记录以保持它们不变,然后添加具有 id self.changed 的​​新元素。即使没有这个,我的意思是只保留 for 循环而没有最后一个 'product_id': self.changed 它不起作用。
  • 有没有办法创建然后附加元素?
  • 检查我的编辑(请注意,您应该指定必填字段)。
  • 我在上面尝试了你的方法,但现在它在 onchange 函数中调用 self.create() 。 AFAIK,它应该只调用 self.move_lines.create() 并且 self.create 只在当前 stock.picking 保存后调用!我不断收到错误:File "/mnt/extra-addons/stock/models/stock_picking.py", line 499, in create move[2]['location_id'] = vals['location_id'] TypeError: 'bool' object does not support item assignment
  • 如何调用'self.create'?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多