Nik
Nik

Reputation: 2726

Odoo UI widget - how to get settings from database?

I'm writing an Odoo v9 widget, which renders a URL, based on concatenation of a setting in the database, and the actual form fields.

The setting in the database I figure should live in ir_config_parameter. I'm inserting a default value with my module.

What's the best way to get this value when rendering the widget? Doing an async ajax call using

new Model("ir.config_parameter")

seems a little heavy handed. Is there a better way to be doing this?

Thanks.

Widget code:

    var UrlWidget2 = form_common.FormWidget.extend({
        start: function() {
            this._super();
            this.field_manager.on("field_changed:ref", this, this.display_result);
            this.display_result();
        },
        display_result: function() {
            var ref = this.field_manager.get_field_value("ref");

            if (!ref) return;

            var baseUrl = 'https://example.com'; //this is the value I want to get from the setting in the database. 
            var url = baseUrl + '/foo/' + ref;    

            this.$el.html('<a href="' + url + '" target="portal">View Externally</a><br /><br/>');
        }
    });

Upvotes: 3

Views: 1028

Answers (1)

Danila Ganchar
Danila Ganchar

Reputation: 11223

You can use RPC for this. This is example which work for me:

var Model = require('web.DataModel'); 

var UrlWidget2 = form_common.FormWidget.extend({
        // just example how to get parameter from backend
        display_result: function() {
            var parameter = new Model('ir.config_parameter');
            // get fields value, key
            parameter.query(['value', 'key'])
                // criteria of search - record with id = 1
                .filter([['id', '=', 1]])
                // only one record
                .limit(1)
                .all()
                .then(function (parameter) {
                    // here data from server
                    console.log(parameter);
            });
        // ...
        }
    });

Hope this helps you.

Upvotes: 0

Related Questions