Odoo 8 - Run ir.actions.act_window from python method?
Is it possible to run ir.actions.act_window from a Python method of the model ?. For example, I have implemented a session model that represents a course session (I also have a course model). Also, I have openacademy.xml where I have defined the form view to open the session form view.
<record model="ir.ui.view" id="session_form_view">
<field name="name">session.form</field>
<field name="model">openacademy.session</field>
<field name="arch" type="xml">
<form>
<header>
<button name="draft" type="workflow"
string="Reset to draft"
states="confirmed,done"/>
<button name="confirm" type="workflow"
string="Confirm" states="draft"
class="oe_highlight"/>
<button name="done" type="workflow"
string="Mark as done" states="confirmed"
class="oe_highlight"/>
<button type="object"
name="my_method"
string="New course"/>
<field name="state" widget="statusbar"/>
</header>
<sheet>
<group string="General">
<field name="name" string="Session name"/>
<field name="course_id" string="Course"/>
<field name="instructor_id" string="Instructor"/>
</group>
<group string="Management">
<field name="start_date" string="Start date"/>
<field name="active" string="Active"/>
<field name="duration" string="Duration (in days)"/>
<field name="seats"/>
<field name="percent_taken_seats" widget="progressbar"/>
</group>
<label for="attendees"/>
<field name="attendees"/>
</sheet>
</form>
</field>
</record>
The relevant part of the above code looks like this:
<button type="object" name="my_method" string="New course"/>
When I click on this button, the my_method method from the Session model (Session class) is called.
class Session(models.Model):
...
@api.one
def my_method(self):
return {
'type': 'ir.actions.act_window',
'res_model': 'openacademy.course',
'view_type': 'form',
'view_mode': 'form',
'target': 'new',
}
This method returns a dictionary containing differents keys and window action values, but it doesn't work in Odoo 8. When I click the New Course button my_method is executed, but the course form form does not open to create a new course record.
I don't know if this can be done in Odoo version 8, or if my_method should return something else.
Decorator problem @api.one
. One decorator wraps up the result of a method in a dict like this:
{res_id: result}
This means that in your case, you are actually returning
{res_id: act_window}
Or in an array if you call it directly: [Act_window]
Unfortunately odoo expects to get the result itself (the actual thing to be returned at some point).
In your case, you should use this:
@api.multi
def my_method(self):
self.ensure_one()
return {
'type': 'ir.actions.act_window',
'res_model': 'openacademy.course',
'view_type': 'form',
'view_mode': 'form',
'target': 'new',
}
self.ensure_one
is not particularly necessary in this case, but it is really important to use @api.multi
. It does not terminate the result with anything and returns exactly what you return, unlike api.one
.
I personally prefer to use api.multi
it because it api.multi
keeps it clean in all cases. When I started using @api.one
I ended up in many places with things like:
val = self._compute_something()[0]
If you need to get the first element because odoo wraps it in a list ... while being @api.multi
more flexible, and if you really want to make it be called with a single element, you can always use ensure_one
.
Try using @ api.multi
I have coded functions that return a window action. The only difference from your code is my decorator. Otherwise use the print in ur function as a debug to make sure it's called