2

I would like to add configuration for a custom Odoo module. However, whenever I try to install the module, I get the following error. It seems that the custom model that I create is not being seen by Odoo.

odoo.exceptions.ValidationError: Error while validating view near:

                            <div class="o_setting_search">
                                <input type="text" class="searchInput pull-right p-0 pb-1" placeholder="Search..."/>
                                <span class="searchIcon"><i class="fa fa-search" role="img" aria-label="Search" title="Search"/></span>
                            </div>
                        </div>

Field "test_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"
    _name = "test.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

2 Answers2

2

Try this.

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,
        })
Neural
  • 370
  • 1
  • 11
  • Can you elaborate why you think your solution will work and why the original solution wasn't working? – CZoellner Dec 01 '21 at 10:42
  • @CZoellner. I think you miss something. I am the one who answered first. But I agree with his solution. – Neural Dec 01 '21 at 10:51
  • 1
    No i don't miss anything but an explanation why your code example should work. I know it's the same solution like in the later answer, but "try this" is not enough for me to upvote an answer. – CZoellner Dec 01 '21 at 12:24
2

You combined _name with inherit attribute which will result in a new model named test.settings. It's called Classical inheritance.

To add the test_field to res.config.settings just remove the _name attribute.

Kenly
  • 24,317
  • 7
  • 44
  • 60
  • 2
    Just as additional information: in now very old Odoo versions it was just normal to create new setting models. But with the big change to one big settings menu, that normal became to not working anymore. So yes in newer versions just inherit `res.config.settings` in an extension way. – CZoellner Dec 01 '21 at 10:45
  • Hi. Thanks. Removing the `_name` attribute from the model *does* allow me to install the module without an error and I even see the Test Field text input field shown on the Settings page. However, when I fill in a value 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? – dloy Dec 01 '21 at 15:57
  • You use the `config_parameter` field attribute – Kenly Dec 02 '21 at 08:57