How can we get login user in ir.action.act.window in odoo v10?

Viewed 2534

We are getting login user in self.env.user, but i want to access login user in ir.action.act.window.

    <record id="act_mail_messages_form_ept_closed" model="ir.actions.act_window">
        <field name="name">Closed</field>
        <field name="res_model">mail.message</field>
        <field name="domain">[('model','=','res.partner'),('res_id','!=',False),('user.company_id','=',company_id)]</field>         
        <field name="context">{'readonly_by_pass': True,'check_domain':True}</field>
        <field name="view_type">form</field>
        <field name="search_view_id" ref="view_message_search"/>
        <field name="view_mode">tree,form</field>
    </record>        

My requirement is I want to filter data without creating record rule, because of if we create record rule of mail.message then system will very slow because for every record system will check record rule.

I want to filter company wise messages using domain in mail.message.

In mail.message i have company_id field (Custom field) and i want to filter data when action is calling.

Is there any alternative solution available to filter message without creating record rule or is there any way from that we can access login user in ir.action.act.window ?

2 Answers

I was in a similar situation where I needed to add domain to show only those results in the tree which are relevant to the logged-in user. This is what worked for me.

XML

<field name="domain">[('cmp_id','=',uid)]</field>   

Python:

cmp_id = fields.Many2one('module.company', related='user_id.company_id')

Here module is the custom module which you have made for company, cmp_id is a Many2one field which is related to the company id through user_id.

Related