设置页面可以保存数值但不显示保存的数值

Settings page can save values but does not display saved values

我正在尝试使用 res.config.settings

创建自定义设置页面

我在 hr addons 文件夹中使用 res_config_settings_views.xml 作为视图的参考。

我也在 hr addons 文件夹中使用 res_config_settings.py 作为模型的参考。

成功了我可以看到我的自定义设置页面。我可以保存这些值。但是在我保存之后,设置页面又回到了未设置的状态。

当我查看数据库时,值已保存。我可以调用该值以在我的模型中使用。

只是困惑为什么设置页面不能显示我保存的值?

我是不是漏掉了什么?

# -*- 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>

您将不得不使用 set_values 方法来保存字段值,然后要再次获取该值,您将不得不使用 get_values 方法。

例如,您在 res.config.settings 模型中有以下字段:

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

现在,为了在单击 Save 按钮后保存此字段值,您必须使用以下代码:

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))

要再次获取字段中的值,请使用以下代码:

@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

希望这段代码对您有所帮助。