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

Hello,

I am using Odoo 15.

I am trying to call do_action from JavaScript. In my do_action call, I need to return an action and a view, but I only know the name of my view.

I tried to do the below rpc call to find my view_id, but it isn't working.

rpc.query({
// Get view id
    model:'ir.model.data',
method:'xmlid_to_res_model_res_id',
args: ['my_module.'my_view_name], // View id goes here
}).then(function(data){
 self.do_action({
name:'view name',
type: 'ir.actions.act_window',
res_model: 'my_model',
view_mode: 'form',
views: [[data[1], 'form']],
})

});

But this throws this error:

*****AttributeError: type object 'ir.model.data' has no attribute 'xmlid_to_res_model_res_id'*****

Please let me know the correct way to make rpc calls in Odoo 15.

Thanks.









Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

Salaam Muhammad Bhai,

Yes it was very helpful.

I finally used this code (with some changes) :

rpc.query({
model: 'ir.model.data',
method: 'search',
args: [
[
['name', '=', 'name_of_my_view']
]
],
}).then(function(view_ids) {
if (view_ids.length > 0) {
var view_id = view_ids['res_id'];
// Do something with the view_id
self.do_action({
name: 'name_of_my_view',
type: 'ir.actions.act_window',
res_model: 'name_of_my_model',
view_mode: 'form',
views: [
[view_id, 'form']
],
})
} else {
console.log("View not found");
}
});

Thanks,

-Varun

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

Nope, I actually used your code itself!

I discarded the above code written by me.
But this website isn't allowing me to edit it due to lack of KARMA!

rpc.query({
model: 'ir.ui.view',
method: 'search',
args: [
[
['name', '=', 'VIEWNAME']
],
],
}).then(function(view_ids) {
if (view_ids.length > 0) {
var view_id = view_ids[0];
// Do something with the view_id
self.do_action({
name: 'VIEWNAME',
type: 'ir.actions.act_window',
res_model: 'MODELNAME',
view_mode: 'form',
views: [
[view_id, 'form']
],
})
} else {
console.log("View not found");
}
});

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

To find the view_id for a specific view using an RPC call from JavaScript in Odoo, you can use the following code:


Copy code

rpc.query({

    model: 'ir.ui.view',

    method: 'search',

    args: [[['name', '=', 'view_name']]],

}).then(function (view_ids) {

    if (view_ids.length > 0) {

        var view_id = view_ids[0];

        // Do something with the view_id

    } else {

        console.log("View not found");

    }

});



In this code, replace 'view_name' with the name of the view for which you want to find the view_id. The code will perform a search for a view with a name matching 'view_name', and if a match is found, it will retrieve the view_id for the first view in the search results.


I hope this helps! Let me know if you have any further questions.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 2 21
3511
1
thg 8 23
411
1
thg 9 21
20580
2
thg 4 21
4454
1
thg 6 18
4403