Search code examples
pythonpython-2.7odoo-8odoo

Pass move_lines into stock.picking from another model - Odoo v8


I have this method:

@api.multi
def create_printy(self):
    copy_record = self.env['stock.picking'] 
    for record in self:

        order_lines = []
        for rec in record.order_lines:
            order_lines.append(
            (0,0,
            {
                'product_id': rec.isbn.id,
                'product_qty': rec.qty,
                }
            ))
        sp_types = self.env['stock.picking.type'].search([
        ('code', '=', 'outgoing')
        ])
        if len(sp_types) > 0:
            copy_record.create({
                'origin': record.name,
                'picking_type_id': sp_types[0].id,
                'move_lines': order_lines, 
                'move_type': 'direct',
                'priority': '1',
                'company_id': record.company_id.id,
            })

I'm trying to create a stock.picking from another model.

But, with this method, I have problems for move_lines which on stock.picking is related to stock.move.

Right now, it throws me this:

        Integrity 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 Unit of Measure - product.uom]

I know there are a few stock.move required fields which aren't present in my order_lines field.

So, my question is, how can I pass for example product.uom or date_expected which are required from my model?

Is there some similar way as it is done with picking_type_id in my example. for One2many field?


Solution

  • You could take a look of what happens when you do not fill these fields in the interface (and look what they get by default). For example, in stock.move Odoo takes the product_uom from product_id field (through an onchange method). And the date_expected is filled in by default with the current date. So:

    @api.multi
    def create_printy(self):
        copy_record = self.env['stock.picking'] 
        for record in self:
            order_lines = []
            for rec in record.order_lines:
                order_lines.append(
                (0,0,
                {
                    'product_id': rec.isbn.id,
                    'product_uom': rec.isbn.uom_id.id,
                    'date_expected': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
                    'product_qty': rec.qty,
                    }
                ))
            sp_types = self.env['stock.picking.type'].search([
            ('code', '=', 'outgoing')
            ])
            if len(sp_types) > 0:
                copy_record.create({
                    'origin': record.name,
                    'picking_type_id': sp_types[0].id,
                    'move_lines': order_lines, 
                    'move_type': 'direct',
                    'priority': '1',
                    'company_id': record.company_id.id,
                })
    

    For date_expected you will have to import in your .py file (out of your classes) the following:

    import time
    from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT