This question has been flagged
2 Replies
22950 Views

hi,

I try to get records of my model in json format in a javascript function.

This is my javascript :

function session() {

odoo.define("sport.session", function (require) {

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

rpc.query({

model: 'sport.session',

method: 'search_session_and_subscription'

}).then(function (data) {

console.log(data);

});

});

}

And this is my python method

@api.model

def search_session_and_subscription(self):

print('search_session_and_subscription')

sessions = []

for session in self:

print('search_session_and_subscription:for.session')

subscriptions = []

for subscription in subscription_ids:

print('search_session_and_subscription:for.subscription')

subscriptions.append(("id", subscription.id))

subscriptions.append(("client_id", subscription.client_id.id))

sessions.append(("id", session.id))

sessions.append(("name", session.name))

sessions.append(("start", session.start_date))

sessions.append(("end", session.end_date))

sessions.append(("color", session.color))

sessions.append(("coach", session.coach_id.name))

sessions.append(("subscriptions", subscriptions))

return json.dumps(sessions)

The result in the browser console is an empty array.

I have write three "print" to see where the function go. And only the first is displayed.

So "self" is empty, but why ?

And how my function can do work ?

Anybody can help me please ?

Avatar
Discard
Author

Ah ! It's works ! Thank you.

Best Answer

Hi,

When calling py from js self contains the object of this model without any records in it. That is why you are using @api.model


You can get all records in your model by using search function,

self.env['your.model.name'].search([])

In your case you can also use  self.search([])

Avatar
Discard

Thanks, this works for me. I had the exact same issue as OP