Odoo 8 - Launch a ir.actions.act_window from a python method? -
is possible launch ir.actions.act_window python method of model?. instance, have implemented session model represents course session (i have course model). also, have openacademy.xml have defined form view open 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 draft" states="confirmed,done"/> <button name="confirm" type="workflow" string="confirm" states="draft" class="oe_highlight"/> <button name="done" type="workflow" string="mark 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 above code following:
<button type="object" name="my_method" string="new course"/>
when click on button method 'my_method' session model (class session) 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 dictionary containing differents keys , values of window action, doesn't work in odoo 8. when click on button "new course", my_method executed course form view not opened create new course record.
i don't know if possible in odoo version 8, or if my_method must return more.
the problem decorator @api.one
. 1 decorator wrap result of method dict this:
{res_id: result}
which means in case, you're returning
{res_id: act_window}
or in array if call directly: [act_window]
unfortunately, odoo expect have result (the actual thing returned @ point).
in case, should instead 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
isn't particularly necessary in case what's important use @api.multi
. doesn't wrap result in , return return unlike api.one
.
personally, prefer use api.multi
because api.multi
keeps things clean in case. when started use @api.one
ended in many place things like:
val = self._compute_something()[0]
where have first element because odoo wraps list... while @api.multi
more flexible , if want force called 1 element can use ensure_one
.
Comments
Post a Comment