0

I would like to add configuration for a custom Odoo module. However, when I fill in a value for the setting test_field and click save, the value does not seem to be saved since when I refresh the page, the value is no longer filled in the input field. Should I change the set_values method on the model? What should I do differently?

This is a follow-up question to this one: Field does not exist in model "res.config.settings".

./models/__init__.py

from . import res_config_settings

./models/res_config_settings.py

from odoo import api, fields, models


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

    test_field = fields.Char(string="Test Field")

    @api.model
    def get_values(self):
        res = super(ResConfigSettings, self).get_values()
        test_config = self.env.ref('test_config', False)
        test_config and res.update(
            test_field=test_config.test_field,
        )
        return res

    def set_values(self):
        super(ResConfigSettings, self).set_values()
        test_config = self.env.ref('test_config', False)
        test_config and test_config.write({
            'test_field': self.test_field,
        })

views/settings.xml

<?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.test</field>
    <field name="model">res.config.settings</field>
    <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="Abcdefg Test Configuration"
          string="Abcdefg Test Configuration"
          data-key="odoo-abcdefg-test-module"
        >
          <h2>Test Configuration</h2>
          <group>
            <div class="row mt16 o_settings_container">
              <div class="o_setting_left_pane">
                <label for="test_field" string="Test Field"/>
                <field name="test_field"/>
              </div>
            </div>
          </group>
        </div>
      </xpath>
    </field>
  </record>
</odoo>

__init__.py

from . import models

__manifest__.py

{
    "name": "ABCDEFG Test Module",
    "version": "0.1.0",
    "category": "Testing",
    "application": False,
    "installable": True,
    "depends": ["base"],
    "data": [
        "views/settings.xml",
    ],
}

What am I doing wrong?

dloy
  • 33
  • 5

1 Answers1

4

From ResConfigSettings source code documentation:

  • For a field with no specific prefix BUT an attribute config_parameter, execute will save its value in an ir.config.parameter (global setting for the database).

  • For the other fields, the method execute invokes set_values. Override it to implement the effect of those fields.

You can override the set_values to save the test_field value in ir.config_parameter table like they did in auth_password_policy module

Example:

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

    test_field = fields.Char(string="Test Field")

    @api.model
    def get_values(self):
        res = super(ResConfigSettings, self).get_values()
        res['test_field'] = self.env['ir.config_parameter'].sudo().get_param("base.test_field", default="")
        return res

    @api.model
    def set_values(self):
        self.env['ir.config_parameter'].set_param("base.test_field", self.test_field or '')
        super(ResConfigSettings, self).set_values()

Or simply using the config_parameter field attribute

Example:

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

    test_field = fields.Char(string="Test Field", config_parameter='base.test_field')
Kenly
  • 24,317
  • 7
  • 44
  • 60