odoo配置界面设置字段默认值

odoo配置界面设置字段默认值 

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

在 Odoo 中定义自定义设置

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

定义模型

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

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

它是一个 TransientModel,也称为向导。不要指望它永久保存这些值。TransientModels 固有地仅在临时基础上存储值。您需要其他方法来使它们永久化。

幸运的是 res.config.settings ,这很容易。首先,您需要为TransientModel 您要定义的每个设置选项添加一些字段 - 一个。Odoo 内置支持四种不同类型的设置。它根据字段名称区分它们。

“默认”设置
命名字段的值 default_foo 将被设置为 作为 参数给出的模型上  命名的字段 的 默认值foodefault_model

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

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

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

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

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.

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 再次无关紧要)。

例如,如果您想使用设置来控制与当前用户关联的公司的名称和电话号码:

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

定义视图

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

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

…当然不要忘记在设置菜单中创建一个新条目:

<menuitem id="your_settings_menu" name="Your settings"
    parent="base.menu_config" action="your_settings_action"/>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值