Search code examples
moduleodooodoo-14warehouse

How can I assign a value to warehouse_id through a custom module in Odoo?


I'm using Odoo 14 community version to test some developments locally. I want to create a custom module that assigns a specific warehouse when the Sale Order is created depending on a logical condition, but first I want to test how to modify values directly

I have tried the next approac, inheriting the sale.order model from the base module sale_stock

My manyfest.py

 'depends': ['base', 'sale_stock'],

My model.py (assigning the wh with id "2" for example)

# -*- coding: utf-8 -*-

from odoo import models, fields, api

class auto_assign_wh(models.Model):
     _inherit = 'sale.order'

     @api.model
     def create(self, vals):   
        vals['warehouse_id'] = 2
        return super().create(vals)

I tried creating a new Sale Order from the website but the WH is still setting to the one with id 1 (the default)

What is the appropriate approach to achieve this?


Solution

  • I solved it using this page as reference to inherit create() method

    My main failure was forget the "api.model" above the method (mainly forgot to uncomment it) and to restart the server in each try

    from odoo import models, fields, api

    class SaleOrder(models.Model):
        _inherit = 'sale.order'
    
        @api.model
        def create(self, vals):   
            vals['warehouse_id'] = 2
            so = super(SaleOrder,self).create(vals)
            return so