Search code examples
pythonpython-2.7odoo-8odoo

Creating stock.picking from another model - Odoo v8


I have this method:

@api.multi
def create_printy(self):
    copy_record = self.env['stock.picking'] # now you call the method directly
    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,
                }
            ))
        copy_record.create({
            'origin': order.name,
            'picking_type_id.id': 'outgoing',
            'move_lines': order_lines, 
            'move_type': 'direct',
            'priority': '1',
            'company_id': record.company_id.id,
        })

This is a button which should create a new stock.picking from my model.

I've tried with picking_type_id.id but it seems to not work, on every example in the standard addons, You just see picking_type_id, also, I don't have any defined in my model, but I though I could pass one of the types available, which is outgoing (the one I need).

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: picking_type_id - picking.type.id]

So, how can I pass this picking_type_id into the stock.picking, should I define this field in my model, even if not required? But it is required by stock.picking model though.


Solution

  • The field picking_type_id is mandatory in stock.picking model, and you are not specifying it in the create method, instead, you are specifying a value for picking_type_id.id, which is not a field (and Odoo is not leting you know this fact). You must find the ID of a stock picking type object, and pass it to the create method. As you want an outgoing type, look for picking types with this code. If you have several warehouses the search method is bound to return several records, that's why I am getting the first one. But if you want another one, you will have to pass more parameters to the search method to be more concise. So try this:

    @api.multi
    def create_printy(self):
        copy_record = self.env['stock.picking'] # now you call the method directly
        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': order.name,
                    'picking_type_id': sp_types[0].id,
                    'move_lines': order_lines, 
                    'move_type': 'direct',
                    'priority': '1',
                    'company_id': record.company_id.id,
                })