转自国外牛人博客: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:

  1. class YourSettings(models.TransientModel):
  2. _inherit = 'res.config.settings'
  3. _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.

  1. class YourSettings(models.TransientModel):
  2. _inherit = 'res.config.settings'
  3. _name = 'your.config.settings'
  4. 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.

  1. class YourSettings(models.TransientModel):
  2. _inherit = 'res.config.settings'
  3. _name = 'your.config.settings'
  4. group_kill = fields.Boolean(
  5. group='your.secret_agents',
  6. implied_group='your.licence_to_kill'
  7. )

“Module” settings
Boolean fields named module_foo, when enabled will trigger installation of a module named foo.

  1. class YourSettings(models.TransientModel):
  2. _inherit = 'res.config.settings'
  3. _name = 'your.config.settings'
  4. # if enabled will install "spies" module
  5. 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:

  1. class YourSettings(models.TransientModel):
  2. _inherit = 'res.config.settings'
  3. _name = 'your.config.settings'
  4. company_name = fields.Char()
  5. company_phone = fields.Char()
  6. @api.model
  7. def get_default_company_values(self, fields):
  8. """
  9. Method argument "fields" is a list of names
  10. of all available fields.
  11. """
  12. company = self.env.user.company_id
  13. return {
  14. 'company_name': company.name,
  15. 'company_phone': company.phone,
  16. }
  17. @api.one
  18. def set_company_values(self):
  19. company = self.env.user.company_id
  20. company.name = self.company_name
  21. 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:

  1. <record id="your_configuration" model="ir.ui.view">
  2. <field name="name">Your configuration</field>
  3. <field name="model">your.config.settings</field>
  4. <field name="arch" type="xml">
  5. <form string="Your configuration" class="oe_form_configuration">
  6. <header>
  7. <button string="Save" type="object"
  8. name="execute" class="oe_highlight"/>
  9. or
  10. <button string="Cancel" type="object"
  11. name="cancel" class="oe_link"/>
  12. </header>
  13. <group string="Company">
  14. <label for="id" string="Name &amp; Phone"/>
  15. <div>
  16. <div>
  17. <label for="company_name"/>
  18. <field name="company_name"/>
  19. </div>
  20. <div>
  21. <label for="company_phone"/>
  22. <field name="company_phone"/>
  23. </div>
  24. </div>
  25. </group>
  26. </form>
  27. </field>
  28. </record>
  1. <record id="your_settings_action" model="ir.actions.act_window">
  2. <field name="name">Your configuration</field>
  3. <field name="res_model">your.config.settings</field>
  4. <field name="view_id" ref="your_configuration"/>
  5. <field name="view_mode">form</field>
  6. <field name="target">inline</field>
  7. </record>

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

  1. <menuitem id="your_settings_menu" name="Your settings"
  2. 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. VC播放mp3的方法

    1.使用msi库 #include <mmsystem.h> #pragma comment(lib,"winmm.lib") ....... //打开文件 MCI_O ...

  2. Arria10收发器校正

    收发器的模拟和数字部分都需要校正来补偿过程,电压和温度(PTV)带来的变化. Arria10使用PreSICE来执行校正过程.   校正主要包括上电校正和用户校正两方面: 上电校正在器件上电时自动执行 ...

  3. 好文推荐系列--------(3)GruntJS 在线重载 提升生产率至新境界

    好文原文地址:http://segmentfault.com/a/1190000000354555 本文将首先介绍grunt-markdown插件如何配合HTML模板使用,接着我将介绍如何使用grun ...

  4. JavaWeb多文件上传及zip打包下载

    项目中经常会使用到文件上传及下载的功能.本篇文章总结场景在JavaWeb环境下,多文件上传及批量打包下载功能,包括前台及后台部分.  首先明确一点:  无法通过页面的无刷新ajax请求,直接发下载.上 ...

  5. python计算机硬件基础以及变量常量常量池,解释器编译器比较,python的两种运行方式

    1.什么是编程语言 语言是一个事物与另外一个事物沟通的介质 编程语言是程序员与计算机沟通的介质 2.什么是编程 编程就是程序按照某种编程语言的语法规范将自己想要让计算机做的事情表达出来 表达的结果就是 ...

  6. ADALINE小demo

    线性逼近 clear;clc;close all x = [1,0.5; 1.5,1.1; 3,3; -1.2,-1]; y = x(:,2); x = [ones(size(x,1),1),x(:, ...

  7. OpenGL中的渐变颜色绘图(应力可视化)

    #include <GL/glut.h> #include <iostream> #include <cmath> using namespace std; ; ; ...

  8. jvm虚拟机---执行引擎子系统

    Java虚拟机只与Class文件相关联,它规定了Class文件应该具有的格式,而不论该文件是由什么语言编写并编译而来.所以,任何语言只要能够最终编译成符合Java虚拟机要求的Class文件,就可以运行 ...

  9. shell wc -l

    shell 命令之 wc -l 给出一个比较常用的命令: cat * | wc -l 查询当前文件夹下的文件的总行数. 原理就是统计了文件中换行符的数量.

  10. AngularJS 指令中的 replace:true

    默认值是fasle,模板会被当作子元素插入到调用此指令的元素内部. <my-directive></my-directive> myModule.directive(" ...