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

Generally in object programming there is something called as Global variables which can be accessed without any object and throughout the program. Now with respect to Odoo, i want to know how and where to define a field which then can be accessed in and from all the other modules.

I am thinking defining a field in base module does the thing but i'm not sure of it. Please answer, if anyone knows how to do this or can this even be done?

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

Yes, you are right: fot that purpose you can apply the class 'BaseModel' from the Odoo core (the file 'models.py').

All other Odoo classes (sale.order, product.product, so any) inherit that class. For example, here the columns '_name', '_order', '_sequence' are defined.

You can define your own as well like _my_global_variable = 100. Then, you may use it in any class as self._my_global_variable. Besides, you will be able to re-define it privately for a definite model.

In many cases, however, it is better to use the model 'ir.config.parameter', and apply to it. It will let you change the value through the interface. For an example have a look at the module 'sale', the file 'res_config_settings.py'. Here you can see how to save and retrieve the global settings. In such a way, for instance, odoo base url is kept.

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

Thanks for the info. +1

Найкраща відповідь

Hi,

you can check this class, you can define field over here, this can be accessible everywhere.

 _inherit = 'res.config.settings'

res[field_name] = self.env['ir.config_parameter'].get_param(key_name, '')

Regards,

Silvestar

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

Thank you for the answer but I have a doubt. What's key_name and the other param inside get_param?

This is github link i have done it in version 10.

i have created this module for website functionality and check models file where i added fields.

and i accessing those fields in the controller .

https://github.com/jsilvestar/odoo/blob/10.0/website_smart_street/controllers/main.py#L19-L20

Regards,

Silvestar

Related Posts Відповіді Переглядів Дія
3
лют. 24
14299
3
трав. 25
1576
5
трав. 25
9379
1
квіт. 25
1178
3
вер. 24
14019