Skip to Content
Menu
This question has been flagged
2 Replies
22999 Views

Hi everyone,

How can I pass the object value fields, or value from my function to window action domain value or context.

Example, I want to pass the "self.ids" and "application_ids" from hr.job to my new window action.

Thank you.

Avatar
Discard
Best Answer

If I understand you correctly, then you are creating a new window by returning from a python function? Then you can add a context into the dictionary that you return, for example:

    @api.multi
    def my_func(self):
        # your code
        return {
            'name': _('Your title'),
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'your_model',
            'res_id': res.id,
            'view_id': view.id,
            'target': 'new',
            'context': {
              'some_key': 'my_data',
            }
        }

Then in your new window, 'my_data' will be accessible via self.env.context['some_key']. Keep in mind, that everything that you return in the dictionary gets converted to JSON, so you can only return basic python types (strings, integers, floats, lists, dictionaries). If you want to pass records, you have to pass their database id.

Avatar
Discard
Author Best Answer

Hi Dan, 

I try to customize the recruitment. In the job position view, when clicking the link "Documents", it will display the documents of the job, or document of applicant applied for that job.

The action for "Documents" link is:

def action_get_attachment_tree_view(self):

        action = self.env.ref('oit_hr_recruitment.oit_hr_job_documents').read()[0]
        action['context'] = {
            'default_res_model': self._name,
            'default_res_id': self.ids[0]
        }
        action['search_view_id'] = (self.env.ref(
            'hr_recruitment.ir_attachment_view_search_inherit_hr_recruitment').id,)
        action['domain'] = ['|', '&', ('res_model', '=', 'hr.job'),
                            ('res_id', 'in', self.ids), '&',
                            ('res_model', '=', 'hr.applicant'), (
                            'res_id', 'in', self.mapped('application_ids').ids)]
        return action

and my action definition:

<record model="ir.actions.act_window" id="oit_hr_job_documents">

            <field name="name">Documents</field>
            <field name="res_model">ir.attachment</field>
            <field name="view_type">form</field>
            <field name="view_mode">kanban,tree,form</field>
            <field name="view_id" eval="False"/>
            <field name="search_view_id" ref="hr_recruitment.ir_attachment_view_search_inherit_hr_recruitment"/>
            <field name="domain">['|','&',('res_model','=','hr.job'),('res_id', '=', active_id),('res_model', '=', 'hr.applicant')]</field>
            <field name="context">{'create': False}</field>
        </record>

It works correctly when user click on "Documents" link, but when press F5 to refresh the page, it will display wrong result, because the domain value is different. 

I know now it's wrong because I don't know how to pass the "self.ids" and "applicant_ids" from hr_job to the action domain value.

Hope that you understand what I said.

Thank you

Avatar
Discard

Have you tried passing the domain only via python and not via xml? Maybe Odoo pulls the domain from XML when you F5, but if you omit it there it will pull it from python (honestly, I am just guessing)?

Also, as a side remark: your function action_get_attachment_tree_view(self) is probably intended to be called only on one recordset (as you take only the first id from self.ids)? In that case, I would add self.ensure_one() at the beginning of the function, as that will ensure you are calling it only on one recordset.

Author

Thank you,

I tried with no domain value on XML but didn't work. And thanks for your remark

Does your function action_get_attachment_tree_view(self) belong to a model or a wizard?

Author

Yes, it's belong to the hr.job

I think that might be a problem. If I understand views correctly, then they are meant to show models (from the database) in certain predefined ways. These can exist without any python code at all. This is your problem, you want to "inject" a domain and a context into the view from python, but odoo only takes the information from the xml (because that is enough for a view to exist). You might try to _not_ define the view in xml and only pass the dictionary with the fully defined view (might take some fiddling). Then all the information should be passed by clicking on the link.