我正在尝试通过 res.config.settings 继承为我的一个模块创建一个设置页面。我阅读了此页面(https://www.odoo.com/de_DE/forum/hilfe-1/question/create-a-custom-configuration-page-for-custom-module-51842)和 Odoo 的文档,我我尝试了很多东西,但没有一个奏效。
我设法设置/获取值,但不可能使它们依赖于多公司。
我在 Ubuntu 18.04.3 LTS 上使用 Odoo 13
有谁知道如何解决这个问题?
这是我的模型:
# -*- coding: utf-8 -*-
from odoo import models, fields, api
from odoo.exceptions import UserError
from odoo import exceptions
class res_config_settings(models.TransientModel):
_inherit = 'res.config.settings'
_check_company_auto = True
is_enabled = fields.Boolean(default=False, string="Is Enabled?", company_dependent=True, check_company=True)
company_id = fields.Many2one('res.company', required=True, default=lambda self: self.env.company)
base_status = fields.Many2one('employee_status.status', string="Default Status", company_dependent=True, check_company=True)
is_reset = fields.Boolean(default=False, string="Enable Reset?", company_dependent=True, check_company=True)
reset_month = fields.Selection(
[('1', 'January'), ('2', 'February'), ('3', 'March'), ('4', 'April'),
('5', 'May'), ('6', 'June'), ('7', 'July'), ('8', 'August'),
('9', 'September'), ('10', 'October'), ('11', 'November'), ('12', 'December'),
], string="Month of the reset", company_dependent=True, check_company=True)
reset_status = fields.Many2one('employee_status.status', string="Reset Default Status", company_dependent=True, check_company=True)
@api.model
@api.depends_context('force_company')
def get_values(self):
res = super(res_config_settings, self).get_values()
is_enabled = self.env['ir.config_parameter'].sudo().get_param('employee_status.is_enabled')
if is_enabled:
base_status = int(self.env['ir.config_parameter'].sudo().get_param('employee_status.base_status'))
is_reset = self.env['ir.config_parameter'].sudo().get_param('employee_status.is_reset')
reset_month = self.env['ir.config_parameter'].sudo().get_param('employee_status.reset_month')
reset_status = int(self.env['ir.config_parameter'].sudo().get_param('employee_status.reset_status')
res.update(
base_status = base_status,
is_reset = is_reset,
reset_month = reset_month,
reset_status = reset_status,
)
return res
@api.model
@api.depends_context('force_company')
def set_values(self):
super(res_config_settings, self).set_values()
param = self.env['ir.config_parameter'].sudo()
if not self.base_status:
raise exceptions.ValidationError('Please give default status.')
if self.is_reset:
if not self.reset_month:
raise exceptions.ValidationError('Please give a month for the reset.')
return
if not self.reset_status:
raise exceptions.ValidationError('Please give a reset status.')
return
field1 = self.base_status and self.base_status.id or False
field2 = self.is_reset or False
field3 = self.reset_month or False
field4 = self.reset_status and self.reset_status.id or False
allEmployees = self.env['hr.employee'].search([])
for employee in allEmployees:
employee.employee_status = field1
param.set_param('employee_status.is_enabled', True)
param.set_param('employee_status.base_status', field1)
param.set_param('employee_status.is_reset', field2)
param.set_param('employee_status.reset_month', field3)
param.set_param('employee_status.reset_status', field4)
和观点:
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="employee_status_settings_view" model="ir.ui.view">
<field name="name">Employee Status Configuration</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="Employee Status Data-String" string="Employee Status" data-key="employee_status">
<h2>Employee Default Status</h2>
<div class="row mt16 o_settings_container">
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="base_status"/>
<span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/>
<div class="row">
<div class="text-muted col-lg-8">
The default Status for your employees.
</div>
</div>
<div class="content-group">
<div class="mt16">
<field name="base_status" required="1"
class="o_light_label" groups="base.group_multi_company"
domain="[('company_id', '=', company_id)]"
context="{'default_company_id': company_id}"/> </div>
</div>
</div>
</div>
</div>
<h2>Status Reset</h2>
<div class="row mt16 o_settings_container">
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="is_reset"/>
<span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/>
<div class="content-group">
<div class="mt16">
<field name="is_reset"
class="o_light_label" groups="base.group_multi_company"
domain="[('company_id', '=', company_id)]"
context="{'default_company_id': company_id}"/>
</div>
</div>
</div>
</div>
</div>
<div class="row mt16 o_settings_container">
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="reset_month"/>
<span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/>
<div class="row">
<div class="text-muted col-lg-8">
Select at which month's begin the reset occurs.
</div>
</div>
<div class="content-group">
<div class="mt16">
<field name="reset_month"
class="o_light_label" groups="base.group_multi_company"
domain="[('company_id', '=', company_id)]"
context="{'default_company_id': company_id}"/>
</div>
</div>
</div>
</div>
</div>
<div class="row mt16 o_settings_container">
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="reset_status"/>
<span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/>
<div class="row">
<div class="text-muted col-lg-8">
Set which status is set when the reset occurs.
</div>
</div>
<div class="content-group">
<div class="mt16">
<field name="reset_status"
class="o_light_label" groups="base.group_multi_company"
domain="[('company_id', '=', company_id)]"
context="{'default_company_id': company_id}"/> </div>
</div>
</div>
</div>
</div>
</div>
</xpath>
</field>
</record>
<record id="employee_status_settings_action" model="ir.actions.act_window">
<field name="name">Employee Status Configuration</field>
<field name="res_model">res.config.settings</field>
<field name="view_id" ref="employee_status_settings_view"/>
<field name="view_mode">form</field>
<field name="target">inline</field>
<field name="context">{'module' : 'employee_status'}</field>
</record>
<menuitem id="employee_status.menu" name="Status"
parent="hr.menu_hr_root"/>
<menuitem id="employee_status.submenu_config" name="Status Configuration"
parent="employee_status.menu"
action="employee_status_settings_action"/>
</odoo>
谢谢你的帮助!