This question has been flagged
6 Replies
35982 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.

Avatar
Discard
Best Answer

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.
Avatar
Discard

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

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

@thanks Grover, your description was helpful

Best Answer

Please help,

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

Avatar
Discard
Best Answer

Hello,

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

Regards

Avatar
Discard