Set and get store data Odoo with TransientModel

ε祈祈猫儿з 提交于 2019-12-04 14:32:26

问题


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

The code use:

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",)

The layout

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


回答1:


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

A simple example:

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



来源:https://stackoverflow.com/questions/35366854/set-and-get-store-data-odoo-with-transientmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!