跳至內容
選單
此問題已被標幟
2 回覆
7042 瀏覽次數

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

頭像
捨棄
最佳答案

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

頭像
捨棄
最佳答案

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

頭像
捨棄
作者

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

相關帖文 回覆 瀏覽次數 活動
1
4月 25
3665
0
3月 24
1402
0
10月 22
2582
0
9月 22
1774
1
7月 22
5449