Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
4 Răspunsuri
8123 Vizualizări

Hello together,


i tried to call a python method from javascript site to pass the data of a form view to javascript.

The rpc call is working fine.

However wenn i try to pass the data of the view to Javascript i get false instead of the data-entry.


Console output:

{'employees': [{'id': False, 'lastName': False}, {'id': False, 'lastName': False}, {'firstName': 'Peter', 'lastName': 'Jones'}]}


Test method: 

 @api.model

    def test(self):

        data = {    "employees":[

                                {"id": self.startpoint_id.id, "lastName": self.startpoint_id.name},

                                {"id": self.endpoint_id.id, "lastName": self.endpoint_id.name},

                                {"firstName": "Peter", "lastName": "Jones"}

                                ],

                    "fruit": "Apple",

                    "size": "Large",

                    "color": "Red"

                }

        json_data = json.dumps(data)

        return json_data


RPC call:

 rpc.query({model: 'selection.menu', method: 'test',

            }).then(function(data){

                console.log('works');

                console.log(data);

});


What am I missing here!?
Any help would be much appreciated

Imagine profil
Abandonează

Please be more accurate about what you mean with "pass the data of the view to Javascript"?, show the code that do that because otherwise it's not clear what are you trying to do

Cel mai bun răspuns

Hi,

Try like below

rpc.query({
model: 'ir.cron',
method: 'search_read',
domain: [ ],
args: [ ],
}).then(function (data) {});

In the above case we can get all the data from the ir.cron inside the function

rpc.query({
'model': 'ir.cron',
'method': 'run_scheduled_actions',
'args': [ ],
});

Python:

def run_scheduled_actions(self):

In the above case we can get all data from the ir.cron in python self.If you want to filter the data use 'args'.

Regards

Imagine profil
Abandonează
Autor Cel mai bun răspuns

@MUHAMMAD Imran Yes var rpc = require('web.rpc'), is integrated in the JS File



To clearify the purpose of this program snippet: 

I have defined a form view to select data from contacts ('res.partner'). Out of this data selection i want to build a json file which contains data and geolocation. Therefore i writted a Python-Method to prepare the data. As next step i want to call the Python method from Javascript to retrieve the Jsondata to JS . Which alt last should be integrated into a Javascript Map Framework.

The testmethod above was only written to check if the orm mapping is working correctly.

Model of the Module

class SelectionMenu(models.Model):

    _name = 'selection.menu'

    _description = 'Run training'


    name = fields.Char(string='Name', required=True, index=True, default=lambda self: ('New'))

    contact_ids = fields.Many2many('res.partner', string="Contacts", required=True)

    startpoint_id = fields.Many2one('res.partner', string="Startpoint")

    endpoint_id = fields.Many2one('res.partner', string="Endpoint")

Imagine profil
Abandonează
Cel mai bun răspuns

Have you included

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

rpc.query({ model: 'model.name', method: 'method_name', args: [{ 'arg1': value1, 'arg2': value2, }] }).then(function (returned_value) { // do something }
Imagine profil
Abandonează
Cel mai bun răspuns

Your code, looks everything  fine.

You can debug python test method is called. (using print statement in test method).

selection.menu object is another model defined add model name and method name.

Example: rpc.query({model: 'model_name.selection.menu', method: 'test', ..............


Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
1
iun. 25
3752
2
nov. 22
5990
1
iun. 22
8069
1
sept. 21
2993
0
aug. 21
95