如何在 Odoo 17 中为自定义模块添加设置菜单

本文详细指导如何在Odoo17中为自定义模块集成设置菜单,包括创建配置模型、定义视图、添加菜单项和窗口操作,以实现用户个性化模块设置。
摘要由CSDN通过智能技术生成

Odoo 是一款极具影响力的开源企业资源规划和业务管理软件,用户可利用自定义模块灵活定制和增强其功能。在定制模块开发中,一个重要的方面是纳入设置菜单,使用户能够根据自己的独特需求对多个选项进行微调。

本文将引导您逐步了解如何在 Odoo 17 中为自定义模块无缝集成设置菜单。这样,用户就可以毫不费力地个性化模块设置。

要启动这一过程,第一步是创建一个自定义模块。这需要定义一个配置模型,负责存放模块的特定设置。该模型继承自 Odoo 提供的基础组件 "res.config.settings "类。打开模块的主 Python 文件并加入以下代码。

class ResConfigSettings(models.TransientModel):
   _inherit = 'res.config.settings'
  
   contract_type = fields.Selection(
       [('monthly', 'Monthly'), ('half_yearly', '6 Months'),
        ('yearly', 'Yearly')],
       string="Contract Type",
       config_parameter='employee_contract.contract_type',
       help="Select contract types from the selection field")

所提供的代码在 "res.config.settings "模型中引入了一个新字段,用于选择员工合同类型。该字段使用了 "config_parameter "属性,该属性利用了可在整个 Odoo 系统中访问的全局模型。该功能便于在数据库中存储和检索配置参数。为确保所选数据得到适当保存,"config_parameter "属性在字段声明中不可或缺。保存记录后,字段中数据的存在或添加情况就会清晰明了。在这种情况下,"employee_contract.contract_type "字段的 "config_parameter "属性就建立起来了,其中 "employee_contract "表示模块名称,"contract_type "表示声明中的字段名称。

继承 "res.config.settings "后,下一步就是为我们的新模块创建视图。为此,请加入以下 XML 代码。

<record id="res_config_settings_view_form" model="ir.ui.view">
   <field name="name">
       res.config.settings.view.form.inherit.employee.contract
   </field>
   <field name="model">res.config.settings</field>
   <field name="priority" eval="15"/>
   <field name="inherit_id" ref="base.res_config_settings_view_form"/>
   <field name="arch" type="xml">
       <xpath expr="//form" position="inside">
           <app data-string="Employee Management" string="Employee Management" name="employee_contract">
               <block title="Employee Management Settings" name="employee_management">
                   <setting string="Contract Type"
                            help="Select the Contract Type"
                            id="contract_type_setting">
                       <field name="contract_type"/>
                       <div class="content-group"
                            invisible="not contract_type"
                            id="group_contract_type_setting">
                           <div class="text-warning mt16">
                               <strong>Save</strong>
                               this page and come back here to set up the
                               feature.
                           </div>
                       </div>
                   </setting>
               </block>
           </app>
       </xpath>
   </field>
</record>

所提供的 XML 代码在 Odoo 框架中为 "res.config.settings "模型定义了一个视图表单,扩展了 ID 为 "base.res_config_settings_view_form "的基本视图。自定义视图引入了 "员工管理 "部分,封装在应用程序和块结构中。在该部分中,定义了 "合同类型 "设置,包括一个用于选择合同类型的字段。XML 代码还包含一条警告信息,根据合同类型的存在有条件地显示,建议用户保存页面并返回进行进一步的功能设置。

随后,加入 "ir.actions.act.window "模型,该模型封装了一个窗口操作,概述了相关菜单项的行为。

<record id="res_config_settings_action" model="ir.actions.act_window">
   <field name="name">Configuration</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' : 'employee_contract'}</field>
</record>
<menuitem id="employee_management_menu_root"
         name="Employee Management"
         sequence="1"/>
<menuitem id="employee_management_menu_action"
         name="Configuration"
         parent="employee_contract.employee_management_menu_root"
         sequence="6"/>
<menuitem id="employee_contract_settings_menu_action"
         name="Settings"
         parent="employee_contract.employee_management_menu_action"
         action="res_config_settings_action"
         sequence="7"/>

上述代码在 Odoo 中为 "res.config.settings "模型建立了操作和菜单项。具体来说,"res_config_settings_action "记录定义了一个名为 "配置 "的窗口操作,其属性包括关联模型、视图模式设置为 "表单 "以及指定上下文。该操作封装了与配置 "员工管理 "设置相关的菜单项的行为。

随后的菜单项构成了应用程序菜单的层次结构。"员工管理 "是根菜单项,其下嵌套着 "配置 "菜单项。此外,"设置 "菜单项与 "res_config_settings_action "操作相关,位于 "配置 "菜单项之下。

按照这些步骤,您就可以执行代码,并观察到字段值在用户界面屏幕上显示为带有预定义选项的选择字段。 

how-to-add-a-settings-menu-for-custom-modules-in-odoo-17-1-cybrosys

在 Odoo 模块中配置所提供的代码是一个简单的过程,需要定义适当的 XML 和 Python 代码。通过这些步骤,您可以显著增强 Odoo 模块的功能和用户体验,为用户提供专门的设置菜单来配置模块的特定选项。这种定制不仅能使模块满足独特的要求,还能改进用户界面,使其更加直观。 

  • 47
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值