Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
26158 Lượt xem

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 ?

Ảnh đại diện
Huỷ bỏ
Tác giả

Ah ! It's works ! Thank you.

Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ

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

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 7 18
8442
0
thg 4 18
2504
0
thg 9 18
3283
3
thg 10 18
14787
2
thg 2 24
15637