Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

Odoo 8 - Launch a ir.actions.act_window from a python method?

By
Eduardo BP
on 5/11/15, 10:09 PM 4,952 views

Is it possible to launch a ir.actions.act_window from a Python method of a model?.

For instance, I have implemented a Session model which represents a course session (I also have a Course model). Also, I have a 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 is the following:

<button type="object" name="my_method" string="New course"/>

When I click on this button a method 'my_method' from the Session model (class Session) 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 return a dictionary containing the differents keys and values of a window action, but it doesn't work in Odoo 8. When I click on the button "New course", my_method is executed but a Course form view is not opened to create a new Course record.

I don't know if it is possible to do this in Odoo version 8, or if my_method must return something more.

EDIT:

Thanks to the @Atchuthan response (profile url: https://www.odoo.com/es_ES/forum/help-1/user/92610), the modified version of my method my_method from the Session model is the following:

class Session(models.Model):
...
@api.multi
def my_method(self):course_form = self.env.ref('openacademy.course_form_view', False) return { 'name': 'New Course', 'type': 'ir.actions.act_window', 'res_model': 'openacademy.course', 'view_type': 'form', 'view_mode': 'form', 'target': 'new', 'views': [(course_form.id, 'form')], 'view_id': 'course_form.id', 'flags': {'action_buttons': True}, }

Thank you very much @Atchuthan. You have my positive vote. Now, the problem is when the course form view pop-up window is opened and I create a new course, the form pop-up window doesn't dissapear. After creating a course in this pop-up window I would like to go back to the form view course (a step back). Is it possible?.

Eduardo BP
on 5/12/15, 8:32 AM
3

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
3883
| 5 3 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 5/12/15, 1:14 AM

The problem is due to the use of @api.one.  If you use @api.multi, then you could open any wizard or so.    

    @api.multi
def my_method(self):
return {
'type': 'ir.actions.act_window',
'res_model': 'openacademy.course',
'view_type': 'form',
'view_mode': 'form',
'target': 'new',
}

Check an example available at Account module in this link.

Thank you very much @Atchuthan. You have my positive vote. Now, the problem is when the course form view pop-up window is opened and I create a new course, the form pop-up window doesn't dissapear. After creating a course in this pop-up window I would like to go back to the form view course (a step back). Is it possible?.

Eduardo BP
on 5/12/15, 8:33 AM

When you click on a button available in the pop-up window, you need to return {'type': 'ir.actions.act_window_close'}

Atchuthan - Technical Consultant, Sodexis Inc
on 5/14/15, 12:58 AM
0
Fernando Gomes
On 5/5/16, 7:54 PM

Hello all.


I am having the same problem. I can generate the popup but the cancel button doesn't do anything and the save button saves the new record but doesn't close the window.

Since I am using the standard popup generated by Odoo I don't have access to the buttons's python code to add the return type ir.actions.act_window_close ...


Any hint on how to make cancel close the popup window and to use "Save & Close" instead of the "Save" button?


Thanks,


Fernando

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 5/11/15, 10:09 PM
Seen: 4952 times
Last updated: 9/3/16, 3:22 AM