Skip to Content
Menu
This question has been flagged
5 Replies
15482 Views

I managed to set up my custom module settings as described by the first answer here

https://www.odoo.com/forum/help-1/question/create-a-custom-configuration-page-for-custom-module-51842

class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
    ...

I read that in this model one are supposed to be able to use these custom settings as default values in other models, but how? I read all the source code on other models from the core, but cant seem to find the answer.

This article (old) states that this is possible

http://ludwiktrammer.github.io/odoo/custom-settings-odoo.html

“Default” settings
The value of a field named default_foo will be set as a default value for a field named foo on a model given as a default_model argument.

..but I can't make it work, so in the mean time I havea default method to set the value from the settings

@api.model
def _get_default_foobar(self):
res = self.env['res.config.settings'].get_values()
return res['foobar'] or 1

which works, but seems like the wrong solution.

Any ideas? Code example would be great.

UPDATE    

default_foo = fields.Integer(default_model='some_model')

I try to set the default value in the model "some_model", but that model's property "foo" does not get the default value.

Avatar
Discard
Author

@Zbik: Thanks for trying, but none of your answers really answers what I'm really asking. I'm looking for a way to set default values in other models from the ResConfigSettings

All solutions are related + default_foo. My answer updated.

Author

Thanks again. Let me just say I _really_ looked into the source code, so I'm familiar with the code you pasted from the source. I added an update to my question, because I tried what you said. In my case I'm trying to set the default value (an integer)

Author

@Hiren Dangar: Thanks, but that will only set the value "foobar" in the ir.config_paramanter model. I already did that. Still doesnt "transfer" the foobar value as default to another model.

In my system default_foo type Integer works without any problems. You must STORE config after set value, onchanage on default_foo is not implemented in this case. Sugestiona: a) you change name "foo" to "xyz" and repeat test. b) You verify, in global settings, values in ir.default (may be here are duplicates)

Author

Thanks again guys, so I got it working in the end when storing the default_foo in the table ir.config_parameter as well as the "foo" value. It didn't make sense to me to store this value more than once, I thought the code could handle it. I mean, lets say I have a value ResConfigSetting that I want to be default in two other models. Then I must basically store 3 values in ir.config_parameter. Anyway, it works so I'm not going to complain :)

Author

urk, so I notice now that I need to save twice in the settings for new defalut value to take affect. After saving once, I see the updated value in ir.config_parameter table, but the default value is still the previous value. Saving one more time will set the default value properly. This is extremely weird

Best Answer

Try this below code.
In odoo version 8, 9 and 10 set default fields as are describe below.

@api.multi

    def set_default_penalty_base(self):

        self.env['ir.values'].set_default('account.config.settings', 'foobar', self.foobar)


In odoo 11 use below code to set default fields.

@api.multi

    def set_values(self):

        super(ResConfigSettingsExd, self).set_values()

        ICPSudo = self.env['ir.config_parameter'].sudo()

        ICPSudo.set_param('jt_penalty_on_due_invoices.foobar', self.foobar)


Avatar
Discard
Best Answer

1  solution - the use field related, example:

    currency_id = fields.Many2one('res.currency', related="company_id.currency_id", required=True,
        string='Currency', help="Main currency of the company.")

2 solution - the use @api.onchange to compute related field
3 solution - inherit method set_values() in class ResConfigSettings
4 solution - on click create wizard and set own values, or use button to call custom method in ResConfigSettings

For example, see addons/account/models/res_config_settings.py  
or the same file in other addons (if exist).

UPDATED ANSWER:

Examples from addons/sale_stock/models/res_config_settings.py (see default_picking_policy):

class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'

security_lead = fields.Float(related='company_id.security_lead', string="Security Lead Time")
group_route_so_lines = fields.Boolean("Order-Specific Routes",
implied_group='sale_stock.group_route_so_lines')
module_sale_order_dates = fields.Boolean("Delivery Date")
group_display_incoterm = fields.Boolean("Incoterms", implied_group='sale_stock.group_display_incoterm')
use_security_lead = fields.Boolean(
string="Security Lead Time for Sales",
oldname='default_new_security_lead',
help="Margin of error for dates promised to customers. Products will be scheduled for delivery that many days earlier than the actual promised date, to cope with unexpected delays in the supply chain.")
default_picking_policy = fields.Selection([
('direct', 'Ship products as soon as available, with back orders'),
('one', 'Ship all products at once')
], "Shipping Management", default='direct', default_model="sale.order", required=True)

@api.onchange('use_security_lead')
def _onchange_use_security_lead(self):
if not self.use_security_lead:
self.security_lead = 0.0

def get_values(self):
res = super(ResConfigSettings, self).get_values()
res.update(
use_security_lead=self.env['ir.config_parameter'].sudo().get_param('sale_stock.use_security_lead')
)
return res

def set_values(self):
super(ResConfigSettings, self).set_values()
self.env['ir.config_parameter'].sudo().set_param('sale_stock.use_security_lead', self.use_security_lead)




Avatar
Discard
Related Posts Replies Views Activity
1
Mar 20
7046
2
Apr 24
2540
1
Mar 15
5470
0
Mar 15
6204
2
Mar 24
6931