Leave a message
Execute a Python Method on Button Click in Odoo
we will demonstrate how to update a student's department using a wizard form in Odoo. The wizard will allow the user to input a new…

In this example, we will demonstrate how to update a student’s department using a wizard form in Odoo. The wizard will allow the user to input a new department name and apply the change to the selected student’s record upon clicking a button.

Wizard Form View with Button

Below is the XML definition of the wizard’s form view, including a button in the footer to trigger the action:

xml

<record id="student_department_view_form" model="ir.ui.view">
    <field name="name">student.department.form</field>
    <field name="model">student.department</field>
    <field name="arch" type="xml">
        <form string="Update Student Department">
            <group class="oe_title">
                <field name="student_name_ids"/>
                <field name="department"/>
            </group>
            <footer>
                <button name="action_update_department" string="Submit" type="object" class="btn-primary"/>
                <button string="Cancel" class="oe_highlight" special="cancel"/>
            </footer>
        </form>
    </field>
</record>
  • name: Refers to the Python method to be executed when the button is clicked (action_update_department in this case).
  • string: The label displayed on the button (e.g., “Submit”).
  • type: Set to object, which triggers the corresponding Python method in the wizard model.

Python Method Execution in Odoo

The following Python code defines the wizard’s functionality. When the “Submit” button is clicked, the action_update_department method is executed, updating the department for the selected student.

python

from odoo import models, fields, api
class StudentDepartmentWizard(models.TransientModel):
    _name = ‘student.department’
    _description = ‘Wizard to Update Student Department’
    department = fields.Char(string="Department", required=True)
    student_name_ids = fields.Many2one(‘student.info’, string=’Student Name’)
    def action_update_department(self):
        for rec in self:
            rec.student_name_ids.department = rec.department

Explanation

  1. Model Definition:
    • student.department: The wizard model to handle temporary data.
    • Fields:
      • department: The new department name entered by the user.
      • student_name_ids: A Many2one field to select a student from the student.info model.
  2. Action on Button Click:
    • The action_update_department method is executed when the “Submit” button is clicked.
    • It updates the department field of the selected student record with the value entered in the wizard.

How It Works

  1. The user opens the wizard and selects a student from the list.
  2. They enter the new department name in the input field.
  3. Clicking the “Submit” button triggers the action_update_department method, applying the change to the selected student’s record.
  4. Refresh the page or view the student record to see the updated department

This example demonstrates how to define and integrate a wizard in Odoo to perform actions based on user inputs. Let me know if you need further assistance!

a