Skip to Content
Menu
This question has been flagged

Hello, I'm trying to pass the value from a custom field I added in stock.move.line to stock.quant when the picking order gets validated but I can't find the function that allows this.


I have the same field already added in the tree view of the model stock.quant.


Thanks in advance.

Avatar
Discard
Best Answer

In Odoo 15, you can pass a value from a custom field in the stock.move.line model to the stock.quant model by using the create() method of the stock.quant model and setting the value of the custom field as an argument.


Here's an example of how you might do this:


Copy code

# Import the necessary models

from odoo import models, fields


class StockMoveLine(models.Model):

    _inherit = 'stock.move.line'


    # Add a custom field

    custom_field = fields.Char(string='Custom Field')


class StockQuant(models.Model):

    _inherit = 'stock.quant'


    # Add a field to receive the value of the custom field

    custom_field_value = fields.Char(string='Custom Field Value')


    def create(self, vals):

        # Set the value of the custom field

        vals['custom_field_value'] = vals['custom_field']

        return super(StockQuant, self).create(vals)

In this example, we have added a custom field custom_field to the stock.move.line model and a field custom_field_value to the stock.quant model to receive the value of the custom field. We have also overridden the create() method of the stock.quant model to set the value of custom_field_value to the value of custom_field when creating a new stock.quant record.


I hope this helps! Let me know if you have any other questions

Avatar
Discard
Author

Thank you for your answer, I tried like you said but it shows an error like this

File "/odoo15/custom/addons/module_stock_custom/models/stock_quant.py", line 15, in create
vals['custom_field_value'] = vals['custom_field']
Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/odoo15/odoo15-server/odoo/http.py", line 643, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/odoo15/odoo15-server/odoo/http.py", line 301, in _handle_exception
raise exception.with_traceback(None) from new_cause
KeyError: 'custom_field'

You know why it happens? And it's different if I try to pass the value of a field many2one instead of a char? Thanks in advance

custom_field_value.name

Pass value like that

Author

Thank you for your answes, I also tried like that but it shows the same error

Related Posts Replies Views Activity
0
Nov 23
388
3
Mar 24
789
0
Nov 21
1384
6
Oct 24
7016
1
Mar 24
689