在Odoo的常规设置中添加 [英] Addition in General Settings of Odoo

查看:101
本文介绍了在Odoo的常规设置中添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写自定义的Odoo模块,具有可由用户设置的一些配置. 我想在
中添加一些设置 设置->配置->常规设置

I am writing as custom Odoo module, with some configuration that can be set by the user. I want to add some setting in
Settings -> Configuration -> General Settings

因此,我创建了一个包含以下内容的 .py :

Therefore, I created a .py containing:

from openerp.osv import fields, osv

class mymodule_configuration(osv.osv_memory):
   _inherit = 'res.config.settings'

  'test_field': fields.char(
                       string='Test Field',
                       required=True,
                             )

.XML

<record id="custom_id" model="ir.ui.view">
   <field name="name">General Settings</field>
   <field name="model">res.config.settings</field>
   <field name="arch" type="xml">
      <form string="General">
         <field name="test_field"/>
      </form>
   </field>
</record>

它不会更改常规设置.

如果我添加参考ID,例如:

If I add the reference id like:

<field name="inherit_id" ref="base_setup.view_general_configuration"/>

然后我得到了错误

ParseError:"ValidateError 字段arch因约束而失败:无效的视图定义

ParseError: "ValidateError Field(s) arch failed against a constraint: Invalid view definition

错误详细信息: 字段module_portal不存在

Error details: Field module_portal does not exist

有人可以帮我解决这个问题吗?

Can anyone help me to sort out this issue?

推荐答案

在Odoo中定义自定义设置

不幸的是,Odoo文档似乎没有包含有关向Odoo添加新配置选项的任何信息.因此,让我们填补空白.

Unfortunately, Odoo documentation doesn’t seem to include any information about adding new configuration options to Odoo. So let’s fill in the gaps.

定义模型

首先,您需要定义一个继承自res.config.settings的新模型:

First of all, you need to define a new model inheriting from res.config.settings:

class YourSettings(models.TransientModel):
    _inherit = 'res.config.settings'
    _name = 'your.config.settings'

这是TransientModel,也称为向导.不要期望它永久保存这些值. TransientModels本质上仅在临时基础上存储值.您需要其他方法使它们永久化.

It’s a TransientModel, also known as a wizard. Do not expect it to permanently hold the values. TransientModels inherently store values on a temporary basis only. You need other means to make them permanent.

幸运的是,res.config.settings make this easy. First of all, you need to add some fields to your TransientModel`-每个要定义的设置选项中的一个. Odoo内置了对四种不同设置的支持.它根据字段名称区分它们.

Fortunately res.config.settings make this easy. First of all, you need to add some fields to yourTransientModel` - one for every setting option you want to define. Odoo comes with built-in support for four different kinds of settings. It distinguishes between them based on the field names.

默认"设置

在给出为default_model参数的模型上,名为default_foo的字段的值将被设置为名为foo的字段的默认值.

The value of a field named default_foo will be set as a default value for a field named foo on a model given as a default_model argument.

class YourSettings(models.TransientModel):
    _inherit = 'res.config.settings'
    _name = 'your.config.settings'

    default_name = fields.Char(default_model='your.other.model')

这将使default_name字段的值成为模型your.other.model中字段name的全局默认值.

This will make the value of default_name field the global default value of a field name in model your.other.model.

组"设置

名为group_foo的布尔字段具有两个参数:group(默认为base.group_user)和implied_group.如果此字段的值为true,则组中定义的组将获得所有implied_group的权限.这与将组添加到另一个组的对象的implied_ids字段完全相同(据我所知,这也是一个未记录的功能).这对于控制哪些用户组可以访问功能很有用.

Boolean fields named group_foo take two arguments: group (defaults to base.group_user) and implied_group. If the value of such a field is true, the group defined in group gain all implied_group’s permissions. This is exactly the same as adding a group to the implied_ids field on another group’s object (which as far as I know is also an undocumented feature). This is useful for controlling which groups of users have access to a feature.

class YourSettings(models.TransientModel):
    _inherit = 'res.config.settings'
    _name = 'your.config.settings'

    group_kill = fields.Boolean(
        group='your.secret_agents',
        implied_group='your.licence_to_kill'
    )

模块"设置

名为module_foo的布尔字段在启用时将触发名为foo的模块的安装.

Boolean fields named module_foo, when enabled will trigger the installation of a module named foo.

class YourSettings(models.TransientModel):
    _inherit = 'res.config.settings'
    _name = 'your.config.settings'

    # if enabled will install "spies" module
    module_spies = fields.Boolean()

其他设置

默认情况下,其他字段的值将被丢弃,但是您可以通过实现自己的保存方式来更改它.只需定义一个名为set_foo的方法(其中foo是任意字符串).您也可以使用get_default_foo方法设置此类字段的初始值(foo的确切形式再次无关紧要).

By default, the values of other fields will be discarded, but you change that by implementing your own means of saving them. Just define a method named set_foo (where foo is an arbitrary string). You can also set initial values of such fields using a get_default_foo method (the exact form of foo is once again irrelevant).

例如,如果要使用设置来控制链接到当前用户的公司的名称和电话号码:

For example, if you want to use settings to control the name and phone number of a company linked to the current user:

class YourSettings(models.TransientModel):
    _inherit = 'res.config.settings'
    _name = 'your.config.settings'

    company_name = fields.Char()
    company_phone = fields.Char()

    @api.model
    def get_default_company_values(self, fields):
    """
    Method argument "fields" is a list of names
    of all available fields.
    """
        company = self.env.user.company_id
        return {
            'company_name': company.name,
            'company_phone': company.phone,
        }

    @api.one
    def set_company_values(self):
        company = self.env.user.company_id
        company.name = self.company_name
        company.phone = self.company_phone

定义视图

然后,您只需要为设置定义一个视图.让我们使用前面的示例:

Then you just need to define a view for your settings. Let’s use the previous example:

<record id="your_configuration" model="ir.ui.view">
    <field name="name">Your configuration</field>
    <field name="model">your.config.settings</field>
    <field name="arch" type="xml">
        <form string="Your configuration" class="oe_form_configuration">
            <header>
                <button string="Save" type="object"
                    name="execute" class="oe_highlight"/>
                or
                <button string="Cancel" type="object"
                    name="cancel" class="oe_link"/>
            </header>
            <group string="Company">
                <label for="id" string="Name &amp; Phone"/>
                <div>
                    <div>
                        <label for="company_name"/>
                        <field name="company_name"/>
                    </div>
                    <div>
                        <label for="company_phone"/>
                        <field name="company_phone"/>
                    </div>
                </div>
            </group>
        </form>
    </field>
</record>

<record id="your_settings_action" model="ir.actions.act_window">
    <field name="name">Your configuration</field>
    <field name="res_model">your.config.settings</field>
    <field name="view_id" ref="your_configuration"/>
    <field name="view_mode">form</field>
    <field name="target">inline</field>
</record>

当然不要忘记在设置菜单中进行新的输入:

and of course don’t forget to make a new entry in the settings menu:

<menuitem id="your_settings_menu" name="Your settings"
    parent="base.menu_config" action="your_settings_action"/>

参考: http://ludwiktrammer.github.io/odoo/custom -settings-odoo.html

这篇关于在Odoo的常规设置中添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆