2
votes

I am extending stock.picking to modify the default value used as location_dest_id.

Standard definition is:

location_dest_id = fields.Many2one(¬
    'stock.location', "Destination Location Zone",¬
    default=lambda self: self.env['stock.picking.type'].browse(self._context.get('default_picking_type_id')).default_location_dest_id,¬
    readonly=True, required=True,¬
    states={'draft': [('readonly', False)]})¬

Which basically uses the default location defined in the picking_type.

I have extended the model so there is a new field in picking_type named force_destination. I would like to set the destination on the stock.picking based on that condition.

Pseudocode:

location_dest_id = fields.Many2one(¬
....
    default=
        if self.env['stock.picking.type'].browse(self._context.get('default_picking_type_id')).force_destination:
            default=1
        else:
            default=lambda self: self.env['stock.picking.type'].browse(self._context.get('default_picking_type_id')).default_location_dest_id

....
2

2 Answers

3
votes

You just need to define a default function:

location_dest_id = fields.Many2one(default="_default_location_dest_id")

@api.model
def _default_location_dest_id(self):
    picking_type_id = self._context.get('default_picking_type_id')
    if not picking_type_id:
        return self.env['stock.location']
    picking_type = self.env['stock.picking.type'].browse(picking_type_id)
    if picking_type.force_destination:
        # return 1  # use an Odoo default external id here
        return self.env.ref('stock.stock_location_stock')
    else:
        return picking_type.default_location_dest_id

I hope this can be helpful for you.

2
votes

Use a default method (default) to compute the default location:

location_dest_id = fields.Many2one('stock.location',
                                   string="Destination Location Zone",
                                   default=lambda self: self._get_default_location(),
                                   readonly=True, 
                                   required=True,
                                   states={'draft': [('readonly', False)]})

@api.model
def _get_default_name(self):
    picking_type = self.env['stock.picking.type'].browse(self._context.get('default_picking_type_id'))
    return picking.force_destination and 1 or picking.default_location_dest_id