Karim Manaouil
Karim Manaouil

Reputation: 1249

odoo validation error when trying to create a stock.move from onchange function

So i want to automatically create a move line (move_lines) in stock.picking from an @onchange function. Here's my function : It's just a small test. When the value of the field changed change, i take it as the id of the product (product_id) in the move line and then append that move line to the already existing list of move_lines.

NB1:move_lines is a One2many relation in stock.picking.

NB2:Declaration of product_id in stock.move :

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

My function :

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

If i created the move lines using the view then saved, everything works fine but when i change the value of the field so that a new move line is inserted by the function, the save doesn't work and i keep getting the error:

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]

What is the problem ?

Upvotes: 1

Views: 1128

Answers (1)

Kenly
Kenly

Reputation: 26708

To append line to move_lines, You can use the following syntax:

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

To achieve it using your above logic you can try x2many values filling but I recommend you to use Set operations and you can find an example at Lunching wizards

Upvotes: 1

Related Questions