Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
1 Відповісти
8220 Переглядів

Hello everyone,

Another newbie question related to Odoo v12.

I need to get the current fields values while creating a new record (create method).

Since I am having some problems doing it and could not find any solution, I am trying to use a global variable for this. The global variable (var_field_value) is set on the top of the module.py outside all classes.

I have tried to use "self.field_name" on create method and it is not working. For instance:

var_field_value = 0
class test (models.Model):
field_name = fields.Integer('Field Value')

On the form view for the class, I enter any integer value to the field_name field.

If I print the field value on the on_change method, I can see the value is there and I can set this value to my global variable:

@api.onchange('field_name')
def onchange_field_name(self):
        print(self.field_name)
        var_field_value = self.field_name
        print(var_field_value)

Until this point, everything runs fine.

Now, on Create method, if I print both "self.field_name" and "var_field_value", the values are 0 for both.

@api.model
def create(self, vals):
    print(var_field_value)
print(self.field_name)
...

So, I can see the variable and field values on on_change event, but I cannot see them on create method.

How can I get the current field values on create method?

Thank you in advance


Аватар
Відмінити
Найкраща відповідь

Hi,

in the onchange method Odoo reads values from the form, in the create method from PostgreSQL. Since in create SQL update is not yet done, self.FIELD returns False.

To get the real field value you can do one of the following:

  1. vals.get('field_name'): all fields values to be created are within the vals dictionary

  2. firstly make super of create, and then apply to the field value you currently do 

Аватар
Відмінити
Автор

Great @Odoo Tools. I was able to solve it using the first option.

The code I have used is:

@api.model

def create(self, vals):

if vals.get('field_name') <=0:

#Desired code here

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

Using this approach as per your instructions, I am able to get the value of field_name on the create method.

Thank you once again

Related Posts Відповіді Переглядів Дія
1
квіт. 25
3251
0
квіт. 24
1805
0
бер. 24
1196
0
жовт. 22
2349
0
вер. 22
1567