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

Hello i'm currently using odoo 14.
What i want to do is to create a window action with  a dynamic record ID "res_id", and this last i want it to be the current user "uid".

<record model="ir.actions.act_window" id="action_window_id">
    <field name="name">my_module.model.action</field>
    <field name="res_model">model.model_one</field>
    <field name="view_mode">form</field>
    <field name="res_id" eval="THE CURRENT UID HERE" />
</record>

i've tried "self.env.uid","obj()"... with no result.
do you have any idea about this !
Thank you very much.

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

I have ended this battle, by steps below:

1- Create method in my model which do the logic for res_id field and return a window action.

def alternative_action(self):
    exist = self.sudo().search([["user_id", "=", self.env.uid]], limit=1)
    # user_id is a field declared into my model
    res_id = self.env.uid
    return {
        "type" : "ir.actions.act_window",
        "res_model" : self._name,
        "view_mode": "form", 
        "res_id": exist.id if exist.id else False
    }

2- Create a server action which will call the method below:

<record model="ir.actions.server" id="id_act_server"> 
    <field name="name">resume res_id action</field> 
    <field name="model_id" ref="module_name.model_name"/> 
    <field name="state">code</field> 
    <field name="code"> 
        action = model.sudo().alternative_action() 
    </field> 
</record>
3- When creating the menu, we call the server action rather than calling the act_window as usual

<menuitem name="My module menu" id="my_module_menu" action="id_act_server"/>

and ... that's it.
This solution may work for different use cases not only mine, we can also give it a title like :
"customize the menu link".

Hopefully this will help some. Thanks

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

Thanks man 

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 7 15
5572
0
thg 3 15
4184
1
thg 1 24
1843
2
thg 3 18
4706
1
thg 3 15
4813