Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openerp context in act_window

Tags:

python

openerp

In OpenERP 6.1 this act_window:

<act_window
     domain="[('id', '=', student)]"
     id="act_schedule_student"
     name="Student"
     res_model="school.student"
     src_model="school.schedule"/>

creates a Student button in the Schedule form which opens the student tree view showing only the appropriate student.

My goal is to directly open the corresponding form view of the student instead of a tree view with the right filtered student. I tried adding a view_mode="form,tree" but it opens a new form instead of the one I want. I'm guessing this can possibly be achieved by adding context to the act_window? Maybe a record_id, but I tried that with active_id and it didn't work.

like image 474
3a2roub Avatar asked Nov 05 '12 15:11

3a2roub


1 Answers

The magical (and probably undocumented) way to have an OpenERP action directly open the form view of a given record, is to set an extra res_id attribute on the action.

Unfortunately in OpenERP 6.1[1] the res_id attribute is not part of the act_window data model, so it is not possible to directly set it in an XML declaration.

Most official addons use a <button type="object" ... /> bound to a Python method that sets the res_id attribute in the returned action. It is quite easy to find examples of this in the source code of official modules, and you can see one in this related question.

Quick/untested example:

You would add this in your school.schedule form:

<button name="open_student_form" type="object" string="Student"/>

And the following method in the school.schedule model:

def open_student_form(self, cr, uid, ids, context=None):
    this = self.browse(cr, uid, ids, context=context)[0]
    return {
        'type': 'ir.actions.act_window',
        'name': 'Student',
        'view_mode': 'form',
        'view_type': 'form',
        'res_model': 'school.student',
        'nodestroy': 'true',
        'res_id': this.student.id, # assuming the many2one is (mis)named 'student'
        'views': [(False, 'form')],
    }

Now if you really wanted to do this with a "sidebar button" (i.e. with an <act_window/>), it gets a bit trickier because you cannot directly bind a sidebar button to a Python method; it must be bound to an action that is stored in the database. It is still doable though, for example via an ir.actions.server action that can be bound to your <act_window/> and calls your Python method or does something similar. The trick with ir.actions.server is that it can be defined as a Python block that can return a dynamic action definition by assigning an action dictionary to an action variable. If you want to follow that path, search the OpenERP addons source code for declarations of ir.actions.server (some of them might do similar things) and methods returning actions with a res_id attribute.

[1] As of OpenERP 7.0 the res_id column is explicitly available in the data model, so you can directly set it.

like image 134
odony Avatar answered Sep 19 '22 23:09

odony