Search code examples
odooodoo-12

Settings page can save values but does not display saved values


I am trying to create a custom settings page using res.config.settings

I'm using res_config_settings_views.xml in the hr addons folder as a reference for the view.

And I'm using res_config_settings.py also in the hr addons folder as a reference for the model.

It worked I can see my custom settings page. I can save the values. But after I saved, the settings page back to its unset state.

When I look at the database, the values were saved. And I can call the value to use in my model.

Just confused why the settings page cannot display the values I saved?

Am I missing something?

# -*- coding: utf-8 -*-

from odoo import fields, models


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

    president_director_id = fields.Many2one(comodel_name='hr.employee', string="President Director", readonly=False)
    head_of_hr_id = fields.Many2one(comodel_name='hr.employee', string="Head of Human Resources", readonly=False)



<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="res_config_settings_view_form" model="ir.ui.view">
        <field name="name">res.config.settings.view.form.inherit.fhid_recruitment</field>
        <field name="model">res.config.settings</field>
        <field name="priority" eval="65"/>
        <field name="inherit_id" ref="base.res_config_settings_view_form"/>
        <field name="arch" type="xml">
            <xpath expr="//div[hasclass('settings')]" position="inside">
                <div class="app_settings_block" data-string="FHID Settings" string="FHID Settings" data-key="fhid_setting">
                    <h2>FHID Settings</h2>
                    <div class="row mt16 o_settings_container">
                        <div class="col-12 col-lg-6 o_setting_box">
                            <div class="o_setting_right_pane">
                                <label for="president_director_id"/>
                                <!-- <span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/> -->
                                <div class="row">
                                    <div class="text-muted col-lg-8">
                                        Set default director, ...
                                    </div>
                                </div>
                                <div class="content-group">
                                    <div class="mt16">
                                        <field name="president_director_id" class="o_light_label"/>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-12 col-lg-6 o_setting_box">
                            <div class="o_setting_right_pane">
                                <label for="head_of_hr_id"/>
                                <!-- <span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/> -->
                                <div class="row">
                                    <div class="text-muted col-lg-8">
                                        Set default manager, ...
                                    </div>
                                </div>
                                <div class="content-group">
                                    <div class="mt16">
                                        <field name="head_of_hr_id"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </xpath>
        </field>
    </record>

    <record id="fhid_recruitment_config_settings_action" model="ir.actions.act_window">
        <field name="name">Settings Manager</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">res.config.settings</field>
        <field name="view_mode">form</field>
        <field name="target">inline</field>
        <field name="context">{'module' : 'fhid_recruitment'}</field>
    </record>

    <menuitem id="fhid_recruitment_menu_configuration"
        name="Settings"
        parent="hr.menu_human_resources_configuration"
        sequence="0"
        action="fhid_recruitment_config_settings_action"
        groups="base.group_system"/>
</odoo>

Solution

  • You will have to use set_values method to save the field values and then to get the value again you will have to you get_values method.

    For example you have the following field in res.config.settings model:

    head_of_hr_id = fields.Many2one('hr.employee', string="Head")
    

    Now in order to save this field value after clicking the Save button you will have to use the following code:

    def set_values(self):
        super(ResConfigSettings, self).set_values()
        set_param = self.env['ir.config_parameter'].sudo().set_param
        set_param('module_name.head_of_hr_id', int(self.head_of_hr_id.id))
    

    To get the value again in the field, use the following code:

    @api.model
    def get_values(self):
        res = super(ResConfigSettings, self).get_values()
        get_param = self.env['ir.config_parameter'].sudo().get_param
        res['head_of_hr_id'] = int(get_param('module_name.head_of_hr_id'))
        return res
    

    Hope this code help you.