تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
2 الردود
26006 أدوات العرض

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 ?

الصورة الرمزية
إهمال
الكاتب

Ah ! It's works ! Thank you.

أفضل إجابة

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

الصورة الرمزية
إهمال

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

المنشورات ذات الصلة الردود أدوات العرض النشاط
2
يوليو 18
8347
0
أبريل 18
2440
0
سبتمبر 18
3204
3
أكتوبر 18
14646
2
فبراير 24
15404