使用TransientModel设置和获取存储数据Odoo [英] Set and get store data Odoo with TransientModel

查看:242
本文介绍了使用TransientModel设置和获取存储数据Odoo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在odoo中存储配置数据,我需要存储3个对'account.journal'的引用. 该模型是在数据库中创建的,视图显示在配置基础菜单中,当我重新加载菜单时,当我按下APPLY按钮但数据未显示时,数据存储在数据库中

I'm trying to store config data in odoo, I need to store 3 reference to 'account.journal'. The model is created in database, the view is shown in configuration base menu, the data is store in database when I push the APPLY button BUT when I reload the menu the data is not shown

代码使用:

from openerp import fields, models, osv, api, _

class Configuration(models.TransientModel):
    _inherit = 'res.config.settings'
    _name = 'transporte_carta_de_porte.config.settings'

    ft_mercaderia = fields.Many2one(
        'account.journal',string='Debito ft mercaderia',
        help="Diario de ajuste al transportista por faltante de mercaderia")
    ade_transportista = fields.Many2one(
        'account.journal',string='Debito por adelanto transportista',
        help="Diario de debito al transportista por faltante de adelanto")
    ade_proveedor = fields.Many2one(
        'account.journal',string='Debito por adelanto proveedor',
        help="Diario de debito por adelanto en la cuenta del proveedor de combustible",)

布局

<record id="view_tcp_config_settings" model="ir.ui.view">
    <field name="name">TCP settings</field>
    <field name="model">transporte_carta_de_porte.config.settings</field>
    <field name="arch" type="xml">
        <form string="TCP settings" class="oe_form_configuration">
            <sheet>
                <div>
                    <button string="Apply" type="object" name="execute" class="oe_highlight" />
                    or
                    <button string="Cancel" type="object" name="cancel" class="oe_link" />
                </div>
                <group string="Journals Settings">
                    <field name="ft_mercaderia" />
                    <field name="ade_transportista" />
                    <field name="ade_proveedor" />
                </group>
            </sheet>
        </form>
    </field>
</record>

<record id="action_tcp_configuration" model="ir.actions.act_window">
    <field name="name">TCP Configuration</field>
    <field name="res_model">transporte_carta_de_porte.config.settings</field>
    <field name="priority" eval="50" />
    <field name="view_mode">form</field>
    <field name="target">inline</field>
</record>

<menuitem id="menu_tcp_config" name="TCP Settings" parent="base.menu_config" action="action_tcp_configuration" />

每次我为字段选择一个值,然后按应用",即创建新记录,却要修改第一个创建的记录,并且视图加载不会加载任何记录. 感谢您的阅读!

Every time I choose a value for the fields and push Apply a new record is created insted of modify the first created and no one is load on the view load. Thanks for reading!

推荐答案

TransientModel被设计为临时的,这样您就可以获取值并随心所欲地对其进行处理.它们会定期从数据库中删除.

TransientModels are designed to be temporary, just so you can get the values and do with them whatever you want to. They are periodically removed from the database.

您需要实现自己的保存这些设置的方法.您需要实现(至少)两种方法:

You need to implement your own means of saving those settings. You need to implement (at least) two methods:

  • set_foo(其中foo是任意字符串)用于保存值.
  • get_default_foo(其中foo再次是任意字符串),用于获取保存的值(用于在配置用户界面中显示它们)
  • set_foo (where foo is an arbitrary string) for saving the values.
  • get_default_foo (where foo once more is an arbitrary string) for getting the saved values (for displaying them in the configuration user interface)

一个简单的例子:

class AgeLimitSetting(models.TransientModel):
    _inherit = 'res.config.settings'

    min_age = fields.Integer(
        string=u"Age limit",
    )

    @api.model
    def get_default_age_values(self, fields):
        conf = self.env['ir.config_parameter']
        return {
            'min_age': int(conf.get_param('age_verification.min_age')),
        }

    @api.one
    def set_age_values(self):
        conf = self.env['ir.config_parameter']
        conf.set_param('age_verification.min_age', str(self.min_age))

ir.config_parameter(提供set_paramget_param方法)只是Odoo中内置的简单键值存储,可让您存储任意字符串.我以它为例,但实际上您可以将设置存储在任意位置.

ir.config_parameter (providing the set_param and get_param methods) is just a simple key-value store built into Odoo that let you store arbitrary strings. I used it as an example, but in reality you can store settings wherever you like.

这篇关于使用TransientModel设置和获取存储数据Odoo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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