Skip to Content
Menú
This question has been flagged
1 Respondre
2742 Vistes

My many2one field was working before but for some reasons, it won't save now when I tried it multiple times. I have declared the field like so:


product_id = fields.Many2one(

        comodel_name="product.product",

        string="Product")

quantity = fields.Float()

price = fields.Float()


I override the create() method of my model because i have additional process to do.


def create(self, values):

       my_model_line = super(MyModel, self).create(values)


       # Insert here my other process -> i do not update my record/line just added a computation here


       return my_model_line



Here is a sample behavior:

1. User created a line

My Line

Product ​Quantity ​Value

[CHAIR] ​     1.0 ​   10.0

2. User saves -> The line was displayed like this

My Line

Product ​Quantity ​Value

​     1.0 ​   10.0

the database is also null


Before, the behavior was correct but i'm not sure now since it's not returning an error. 




Avatar
Descartar

Sorry I don't have enough details to see why it's not working. It's easier if you share the module link

Best Answer

Hi,

In the create() method, you should not directly modify the values dictionary that is passed as an argument because it can interfere with the creation process and lead to unexpected behavior.
You can use the write() function to update the newly formed record after it has been created in order to correctly handle additional computations throughout the create process without directly altering the values dictionary. Here is how to go about it:
def create(self, values):
        my_model_line = super(MyModel, self).create(values)

        # Additional computation goes here
        # Example: Calculate the value based on quantity and price
        value = values.get('quantity', 0.0) * values.get('price', 0.0)
       
        # Update the value field of the newly created record
        my_model_line.write({'value': value})

        return my_model_line
This method uses the write() method to update the newly formed record's 'value' field rather than directly altering the values dictionary during the initial construction process. By doing this, you can make sure that the Many2one field and other fields behave correctly while new records are being created.

Hope it helps

Avatar
Descartar
Related Posts Respostes Vistes Activitat
1
de maig 23
3382
2
de maig 23
3113
1
de gen. 22
3277
0
de des. 15
5086
0
de març 15
4442