Skip to Content
Menu
This question has been flagged
1 Reply
5959 Views

Hello everyone, I try to call python function by model without rpc , but when I declare model, I see this error ReferenceError: instance is not defined

Here is my js code

odoo.define('erpvn_ztree_view.Model'function (require) {
    "use strict";

    var AbstractModel = require('web.AbstractModel');

    var ZtreeModel = AbstractModel.extend({
       
        getfunction () {
            return this.data;
        },
        /**
         * @param {Object} params
         * @param {string} params.modelName the name of the model
         * @returns {Deferred} The deferred resolves to some kind of handle
         */
        loadfunction (params) {
            this.modelName = params.modelName;
            this.domain = params.domain || [];
            this.fields = params.fields;
            this.viewFields = params.viewFields;
            this.child_id = params.child_id;
            this.parent_name = params.parent_name;
            this.parents_name = params.parents_name;
    
            
            return this._fetchData();
        },
        /**
         * @param {Object} params
         * @returns {Deferred}
         */
        reloadfunction (handleparams) {
            if ('domain' in params) {
                this.domain = params.domain;
            }
            return this._fetchData();
        },
        
        _fetchDatafunction () {
            var self = this;
            var params = Array.prototype.slice.call(arguments);
            var recordId = params[0];
            var child =[];
            var defs = [
                this._rpc({
                    model:this.fields[this.parent_name].relation,
                    method'search_read',
                    sort"",
                })
            ];
            return $.when.apply($defs,).then(function () {
                var results = Array.prototype.slice.call(arguments);
                console.log(child);

                var model = new instance.web.Model("custom.model"); // Error here
                model.call("my_function", {
                    context: new instance.web.CompoundContext()
                    }).then(function(result) {
                    console.log(result);
                    });
                
                self.data = {
                    "zTreeNodes": results[0],
                };
            });
        },

    });

    return ZtreeModel;

});      
Here is my model
 
from odoo import fields, models,api
class CustomModel(models.Model):
    _name = 'custom.model'

    @api.model
    def my_function(self):
        return 'Hello world'

Avatar
Discard
Best Answer

You already know how to do it as you have already used "this._rpc". The same way you can call the method of your model.

this._rpc({
model: 'custom.model',
method: 'my_function',
args: [],
})).then(function (result) {
console.log("result... ", result)
})


Avatar
Discard
Related Posts Replies Views Activity
0
May 23
1839
1
Jan 23
6355
1
Jun 24
2345
0
Jun 23
1231
1
May 23
1907