SallySalty
SallySalty

Reputation: 35

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?

Upvotes: 0

Views: 156

Answers (2)

Levizar
Levizar

Reputation: 85

It is because you are creating a sale order but your code is creating a new model that isn't used.

If you want to "override" sale.order, you need to do this:

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

That way, it would apply to all sale.order model.

If you want to use your current code with your new model, you should know that sale.order would be independant of it and that you need to create a view for auto_assign_wh.auto_assign_wh to make it accessible.

Regarding how to change warehouse_id: it is a computed field so the way to go would be to override the _compute_warehouse_id.

Take care when modifying such methods, you could break the initial implementation Odoo intended to make.

Upvotes: 0

SallySalty
SallySalty

Reputation: 35

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
    

Upvotes: 0

Related Questions