0

我想为自定义 Odoo 模块添加配置。但是,当我为设置填写一个值test_field并单击保存时,该值似乎没有被保存,因为当我刷新页面时,该值不再填写在输入字段中。我应该更改set_values模型上的方法吗?我应该怎么做?

这是对此问题的后续问题:模型 "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",
    ],
}

我究竟做错了什么?

4

1 回答 1

0

来自ResConfigSettings源代码文档:

  • 对于没有特定前缀但属性的字段config_parameterexecute会将其值保存在ir.config.parameter(数据库的全局设置)中。

  • 对于其他字段,该方法execute调用set_values. 覆盖它以实现这些字段的效果。

您可以像在auth_password_policy模块中那样覆盖set_values以将test_field值保存在表中ir.config_parameter

例子:

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

或者简单地使用config_parameterfield 属性

示例

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

    test_field = fields.Char(string="Test Field", config_parameter='base.test_field')
于 2021-12-02T08:57:32.610 回答