Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

5

How can I save/load my own configuration/settings

By
Stefan Reisich
on 9/4/13, 8:09 AM 13,706 views

Hello,

how can I save and load my own configuration/settings. And create my own section in Settings -> Configuration? How can I load this saved settings?

I want save a URL, a user name and a password needed for my module...

image description

Thank you very much.

11

Grover Menacho

--Grover Menacho--
2523
| 5 7 8
La Paz, Bolivia
--Grover Menacho--

Systems Engineer + MBA

Grover Menacho
On 9/5/13, 12:23 AM

To add a configuration page you have to do this:

from openerp.osv import fields, osv 

class custom_config_settings(osv.osv_memory):

    _name = 'custom.config.settings'
    _inherit = 'res.config.settings'
    _columns = {
        'username': fields.char('Username', size=48),
    }
    _defaults = {
        'username': ""
    }

    def get_default_username(self, cr, uid, fields, context=None):
        #some code... 
        #you can get the field content from some table and return it
        #as a example
        user_name=self.pool.get('res.users').browse(cr, uid, uid, context=context).name
        return {'username': user_name}

    def set_default_username(self, cr, uid, ids, context=None):
        #some code... 
        #you can get the field content from some table and return it
        #as a example
        config = self.browse(cr, uid, ids[0], context)
        new_username=config.username
        self.pool.get('res.users').write(cr, uid, uid, {'name': new_username})

And the view:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
    <record id="view_custom_config_settings" model="ir.ui.view">
        <field name="name">custom settings</field>
        <field name="model">custom.config.settings</field>
        <field name="arch" type="xml">
            <form string="Configure Accounting" version="7.0" class="oe_form_configuration">
                <header>
                    <button string="Apply" type="object" name="execute" class="oe_highlight"/>
                    or
                    <button string="Cancel" type="object" name="cancel" class="oe_link"/>
                </header>
                <field name="username"/>
            </form>
        </field>
    </record>

    <record id="action_custom_config" model="ir.actions.act_window">
        <field name="name">Custom Settings</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">custom.config.settings</field>
        <field name="view_mode">form</field>
        <field name="target">inline</field>
    </record>

    <menuitem id="menu_custom_config" name="Custom Settings" parent="base.menu_config"
        sequence="16" action="action_custom_config"/>

    </data>
</openerp>

This is only an example.

The config page works because when you open the configuration page all the "get_" functions will be called. And when you save it all the "set_" functions are going to run.

I hope this can help you.

EDIT

Taken from OpenERP docs:

Base configuration wizard for application settings. It provides support for setting default values, assigning groups to employee users, and installing modules. To make such a 'settings' wizard, define a model like::

        class my_config_wizard(osv.osv_memory):
            _name = 'my.settings'
            _inherit = 'res.config.settings'
            _columns = {
                'default_foo': fields.type(..., default_model='my.model'),
                'group_bar': fields.boolean(..., group='base.group_user', implied_group='my.group'),
                'module_baz': fields.boolean(...),
                'other_field': fields.type(...),
            }

The method execute provides some support based on a naming convention:

*   For a field like 'default_XXX', ``execute`` sets the (global) default value of
    the field 'XXX' in the model named by ``default_model`` to the field's value.

*   For a boolean field like 'group_XXX', ``execute`` adds/removes 'implied_group'
    to/from the implied groups of 'group', depending on the field's value.
    By default 'group' is the group Employee.  Groups are given by their xml id.

*   For a boolean field like 'module_XXX', ``execute`` triggers the immediate
    installation of the module named 'XXX' if the field has value ``True``.

*   For the other fields, the method ``execute`` invokes all methods with a name
    that starts with 'set_'; such methods can be defined to implement the effect
    of those fields.

The method ``default_get`` retrieves values that reflect the current status of the
fields like 'default_XXX', 'group_XXX' and 'module_XXX'.  It also invokes all methods
with a name that starts with 'get_default_'; such methods can be defined to provide
current values for other fields.

@Grover, in many configuration settings available in OpenERP, most of the field does not have get or set function with it

For instance take sales configuration, `module_sale_analytic_plans,

Atchuthan - Technical Consultant, Sodexis Inc
on 12/13/13, 1:07 PM

Of course, but it depends on what you are trying to do: If field name starts with module_ that means that it's going to install that module. It's not an ordinary boolean. I've edited the answer. Hope it helps

GH Mediacloud, Grover Menacho
on 12/13/13, 4:20 PM

@thanks Grover, your description was helpful

Atchuthan - Technical Consultant, Sodexis Inc
on 12/18/13, 9:53 AM
0
Iwan Dermawan
On 11/21/16, 11:42 PM

Please help,

why in my configurations, new record added after Apply button clicked not updated last config data (reload record succesfull display in form)

-2

Thierry Godin

--Thierry Godin--
1516
| 8 7 8
Collegien, France
--Thierry Godin--

..

Thierry Godin
On 9/4/13, 6:30 PM

Hello,

Your configuration will be saved while you're making backup of your database.

Regards

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)

Stats

Asked: 9/4/13, 8:09 AM
Seen: 13706 times
Last updated: 11/21/16, 11:42 PM