odoo 增加自定義配置項 一 get_value set_value

這種配置項適合的場景在於全局的配置項,比如我這個系統的title 就設置成統一的東西。

繼承 res.config.settings ,增加配置項字段。

繼承set_value方法以設定存儲的地點,而get_value中設置獲取值的方法

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

# -------------------------------------------------------------------------------
# Author:       CAO.T.F
# Date:         2019/10/7 10:59
# Description:  系統設置項

# -------------------------------------------------------------------------------

from odoo import models, fields, api


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

    system_name = fields.Char('System Name', help=u"Setup System Name,which replace Odoo")

    @api.model
    def get_values(self):
        res = super(ResConfigSettings, self).get_values()
        ir_config = self.env['ir.config_parameter'].sudo()
        system_name = ir_config.get_param('system_name', default='默認odoo')

        res.update(
            system_name=system_name,
        )
        return res

    @api.model
    def set_values(self):
        super(ResConfigSettings, self).set_values()
        ir_config = self.env['ir.config_parameter'].sudo()
        ir_config.set_param("system_name", self.system_name or "")
        super(ResConfigSettings, self).set_values()

新建視圖

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <record id="view_app_theme_config_settings" model="ir.ui.view">
            <field name="name">Odoo Customize Settings</field>
            <field name="model">res.config.settings</field>
            <field name="priority">20</field>
            <field name="arch" type="xml">
                <form string="odooApp Customize Settings" class="oe_form_configuration o_base_settings">
                    <div class="o_control_panel">
                        <div class="o_panel">
                            <div class="title h2 mb0 mt4 text-muted d-none d-md-block">Customize Settings</div>
                        </div>
                        <header>
                            <button string="Apply" type="object" name="execute" class="oe_highlight"/>
                            <button string="Cancel" type="object" name="cancel" class="oe_link" special="cancel"/>
                        </header>
                    </div>
                    <div style="padding:0 200px;">
                        <h2>White Label Setting</h2>
                        <group string="System Name">
                            <label for="system_name"/>
                            <div>
                                <field name="system_name"/>
                                <div>
                                    <a href="https://www.baidu.com" target="_blank">Visit our website for more apps and
                                        Support. https://www.baidu.com
                                    </a>
                                </div>
                            </div>
                        </group>
                    </div>
                </form>
            </field>
        </record>

        <record id="action_app_theme_config" model="ir.actions.act_window">
            <field name="name">Customize Debrand</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="view_id" ref="view_app_theme_config_settings"/>
            <field name="target">inline</field>
        </record>

        <menuitem
                id="base.menu_app_group"
                name="odooApp"
                parent="base.menu_administration"
                sequence="1"
                groups="base.group_system"/>
        <menuitem
                id="menu_app_theme_config"
                parent="base.menu_app_group"
                sequence="1"
                action="action_app_theme_config"
                groups="base.group_system"/>
    </data>
</odoo>


當打開配置界面時系統會自動調用res.config.settings類的default_get方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章