コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1 返信
2790 ビュー

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. 




アバター
破棄

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

最善の回答

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

アバター
破棄
関連投稿 返信 ビュー 活動
1
5月 23
3442
2
5月 23
3163
1
1月 22
3325
0
12月 15
5143
0
3月 15
4492