转自国外牛人博客:http://ludwiktrammer.github.io/odoo/custom-settings-odoo.html

Defining custom settings in 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.

Defining a model

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'

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.

Fortunately res.config.settings makes this easy. First of all, you need to add some fields to your TransientModel - 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” settings
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')

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

“Group” settings
Boolean fields named group_foo take two arguments: group (defaults to base.group_user) and implied_group. If the value of such field is true, the group defined in group gain all implied_group’s permissions. This is exactly the same as adding a group to 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” settings
Boolean fields named module_foo, when enabled will trigger 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()

Other settings
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

Defining a view

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"/>

odoo配置界面设置字段默认值的更多相关文章

  1. Odoo14 设置Binary字段默认值

    1 # Odoo 中的附件也就是Binary字段都是经过特殊处理的 2 # 首先是上传的时候会进行base64编码后再上传到服务器 3 # 服务器进行压缩存放在odoo文件仓库中 4 # 每个odoo ...

  2. Mysql 修改字段默认值

    环境:MySQL 5.7.13 问题描述:建表的时候,users_info表的role_id字段没有默认值,后期发现注册的时候,需要提供给用户一个默认角色,也就是给role_id字段一个默认值. 当前 ...

  3. SQL Server2000导出数据时包含主键、字段默认值、描述等信息

    时经常用SQL Server2000自带的导出数据向导将数据从一台数据库服务器导出到另一台数据库服务器: 结果数据导出了,但表的主键.字段默认值.描述等信息却未能导出,一直没想出什么方法,今天又尝试了 ...

  4. struts中的常量,action配置中的默认值

    1.struts中Action的开发方式 继承ActionSupport类,这种方法实现的Action可以进行数据校验: 实现Action接口: 不继承任何类,不实现任何接口: 是否继承类或实现接口, ...

  5. mysql字段默认值不生效的问题解决(上)

    在项目中使用mybatis做为持久层框架,mysql数据库.项目上线前,DBA要求我们将每张数据库表中的字段都设置默认值和not null.之前项目中有一些insert语句是将表中所有字段都列出来,然 ...

  6. SQL修改字段默认值、获取字段默认值

    一.SQL修改字段默认值 alter table 表名 drop constraint 约束名字 说明:删除表的字段的原有约束 alter table 表名 add constraint 约束名字 D ...

  7. Sqlserver添加加字段、删除字段、修改字段类型、修改字段名、修改字段默认值

    参考:https://www.cnblogs.com/pangpanghuan/p/6432331.html 初始化表: --.添加字段 --1.1.为null alter table DataTab ...

  8. MySQL字段默认值设置详解

    前言: 在 MySQL 中,我们可以为表字段设置默认值,在表中插入一条新记录时,如果没有为某个字段赋值,系统就会自动为这个字段插入默认值.关于默认值,有些知识还是需要了解的,本篇文章我们一起来学习下字 ...

  9. sql 修改字段默认值

    1.查出该字段的约束名称 SELECT c.name FROM sysconstraints a INNER JOIN syscolumns b on a.colid=b.colid INNER JO ...

随机推荐

  1. python3 print函数的用法

    1. 输出字符串 >>> strHello = 'Hello World' >>> print (strHello) Hello World 2. 格式化输出整数 ...

  2. idea中Eclipse Code Formatter插件设置和使用,以及注释模板的修改

    在settings里面找到plugins这个选项,搜索Eclipse Code Formatter,点击安装,重启idea即可进行配置: 首先,先安装Eclipse Code Formatter插件: ...

  3. 如何制作chm文件

    本文介绍如何从一个包中的docs文档生成一个chm文档. 1,准备软件Easy CHM 这个网上有下载,下载后安装,至于怎么使用,等下再介绍.安装之后如下图. 2,准备文件 比如我这里下载了一个cxf ...

  4. php 16进制颜色代码转换为rgba,rgb格式

    <?php $rgb = hex2rgba('#FFFFFF', false, true); echo 'rgb: '.$rgb[0].','; echo $rgb[1].','; echo $ ...

  5. CentOS 利用Yum安装mysql后无法启动(MySQL Daemon failed to start.)

    安装mysql-server [root@iZwz9cl4i8oy1reej7o8pmZ ~]# yum install -y mysql-server 进入/etc执行mysql_install_d ...

  6. 20145232 韩文浩 《Java程序设计》第3周学习总结

    教材学习内容总结 在第三章中,知道了Java可区分为基本类型和类类型两大类型系统,其中类类型也称为参考类型.在这一周主要学习了类类型. 对象(Object):存在的具体实体,具有明确的状态和行为 类( ...

  7. Qt_简介

    Qt简介: 1990 开发 1991 发布Qt 1.0. 公司:Trolltech (奇趣科技) 1997 Qt被用来开发Linux桌面KDE 2008 被Nokia收购 2012 被转让给Digia ...

  8. android Activity中设置setResult 没有触发onActivityResult

    昨天修改<manifest 文件中activity 的 模式为单例模式:android:launchMode="singleTask" ,发现我的onActivityResu ...

  9. android PendingIntent 使用通知传递多个参数,及不覆盖的问题

    Intent updateIntent = new Intent(GetNoticeService.this, DetailGonggaoActivity.class); updateIntent.p ...

  10. 修改Android EditText光标颜色

    EditText有一个属性:android:textCursorDrawable,这个属性是用来控制光标颜色的   android:textCursorDrawable="@null&quo ...