This question has been flagged
2 Replies
5012 Views

Hello everyone,

In Odoo 12 we can directly access company model data on field definition like for instance:

my_field = fields.Char('Test field', default=lambda self: self.env.user.company_id.my_field)

This way we have access to desired values set on company model.

What about if I want to directly access values on "ir_config_parameters" (not on the res_company model) from field definition?

Something like:

my_field = fields.Char(default=lambda self: direct_access_to_ir_config_parameters_here)

Thank you all in advance

Best regards

PM

Avatar
Discard
Best Answer

Hello paulo,

you can do like:


def get_my_field_value(self):
config = self.env['ir.config_parameter']
return config.sudo().get_param("key_to_fetch_value")

my_field = fields.Char('Test field', default=get_my_field_value)


For more detail just check this link: https://odoo-development.readthedocs.io/en/latest/dev/py/res.config.settings.html

Avatar
Discard
Best Answer

Hello Paulo,

You need to define the same field in "res.company" model which defines in "res.config.settings" model like this.

https://github.com/odoo/odoo/blob/13.0/addons/account/models/company.py#L56


And you need to make a field related to res company's field. It should be defined in "res.config.settings" like this.

https://github.com/odoo/odoo/blob/13.0/addons/account/models/res_config_settings.py#L13


Also update the functions get_values() and set_values() according to this field.

Now you can use the same approach for accessing "ir_config_parameters" values like this.

https://github.com/odoo/odoo/blob/13.0/addons/account/models/account_move.py#L4138


Feel free to ask in case of any query.


Thanks,

Aman Prakash,

Webkul Software Private Limited

Avatar
Discard
Author

Thank you @Aman Prakash,

You're answer is correct and I have not asked correctly. What I wanted to do, is to use a direct access method to values on "ir_config_paramenters" on field definition as I have done with "default=lambda...".

The value I want to access is not on res_company.

Anyway I have edited my question

Thank you very much

PM