Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
26105 Widoki

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 ?

Awatar
Odrzuć
Autor

Ah ! It's works ! Thank you.

Najlepsza odpowiedź

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([])

Awatar
Odrzuć

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

Powiązane posty Odpowiedzi Widoki Czynność
2
lip 18
8416
0
kwi 18
2466
0
wrz 18
3267
3
paź 18
14741
2
lut 24
15599