Video summary

Odoo 17 Development: Sequence, No update Attribute, Rec Name and Create Method

Main summary

Key takeaways

Educational

Main ideas / concepts taught (Odoo 17 module development)

Recap of what was built earlier

The earlier videos covered building a basic Odoo module, including:

  • Models
  • Menus
  • Form and tree views
  • Window actions
  • Model access rights

Creating a hospital appointment model (new business model)

In the HMS (Hospital Management System) example, the video adds a model to record outpatient appointments:

  • Model name: hospital.appointment

Key steps covered:

  1. Create a new Python model file (example: appointment.py) by copying an existing model file (example: patient.py) and renaming it.
  2. Import the new file in the module’s __init__.py.
  3. Define model fields including:
    • A many-to-one relationship to patients
    • Appointment date
    • Appointment time (date/time field)
    • Notes/call details (text field)
  4. Add model security/access configuration (briefly referenced as “define security” / “set its access”).

Defining a Many-to-One field correctly

A many-to-one field links an appointment to a patient (patient selection dropdown). The video emphasized:

  • Naming rule: the technical field name should end with _id
    • Example: patient_id
  • Configuration rule: specify the base model that provides selectable records
    • Example: hospital.patient

UI behavior mentioned:

  • Without configuring rec_name, the list may display the model name and numeric record IDs instead of a friendly patient name.

Using rec_name to control how records display

The issue shown:

  • Appointment records displayed as: model name + record ID (because record name wasn’t defined)

The solution taught:

  • Set rec_name so the appointment display name comes from a relevant field tied to the patient relation (e.g., showing the patient’s name from patient_id).

Result after the change:

  • The UI shows patient-friendly values (e.g., “Sam George”) instead of IDs.

Generating a consistent appointment “reference/number” using Odoo Sequences

Motivation:

  • Each appointment should have a stable identifier (similar to sales order/quote numbers).

Detailed steps:

  1. Create a sequence record in XML
    • Add a new directory: data
    • Create sequence.xml
    • Import it in __manifest__.py
  2. Follow the Odoo example pattern for XML sequence definition.
  3. Configure sequence values:
    • Sequence code tied to the model (appointment reference sequence code)
    • Prefix (example: HP)
    • Number padding (example: 5 digits)
    • Start increment at 1
  4. Add a new field to the appointment model:
    • A Char field (example: reference)
    • Default placeholder initially set to "new"
  5. Make this reference visible in views:
    • Add it to the form/tree view XML
    • Set it read-only after auto-generation:
      • readonly="true"
  6. Auto-assign it by overriding create:
    • Use @api.model_create_multi
    • Override logic:
      • Iterate through vals_list
      • When reference is still "new" (or empty), assign:
        • self.env['ir.sequence'].next_by_code(<sequence_code>)
      • Then call:
        • return super().create(vals_list)
  7. Debugging step:
    • Print vals_list to verify incoming values during record creation.

Fixing sequence assignment edge cases

The video notes an edge case where vals['reference'] could be empty or not set as expected. The adjustment:

  • Assign the sequence when the field is missing or still "new" (i.e., not yet generated).

Preventing sequence reset on module updates: noupdate attribute

Problem:

  • If the sequence XML runs again during module updates, prefix/sequence state can revert to XML defaults.

Solution:

  • Use noupdate="1" on the XML data record(s).

Behavior demonstrated:

  • Without noupdate (default is effectively false): updates can overwrite existing sequence configuration.
  • With noupdate="1": the already-created sequence record persists and is not overwritten.

Practical example flow:

  • Change prefix from HP to AP in the UI.
  • Show that an update can revert it without protection.
  • Add noupdate, then confirm the UI-changed prefix remains stable after updates.

Implementation guidance:

  • Add noupdate="1" in the XML (or use a <data> tag with noupdate="1").
  • Mentioned editing tip: use Ctrl+Alt+L / formatting during XML editing.

Methodology / instruction lists (explicit steps)

A) Build the hospital.appointment model

  • Create appointment.py:
    • Copy an existing model (e.g., patient.py)
    • Rename appropriately
  • Update __init__.py:
    • Import the new appointment.py module file
  • Define model hospital.appointment:
    • Inherit mail thread/related features (video references inheriting mail.thread / mail flow)
    • Set model description
  • Add fields:
    • Many-to-one: patient_id
      • Technical naming ends with _id
      • Field type: many-to-one
      • Comodel: patient model (e.g., hospital.patient)
      • Label: e.g., “patient”
    • Date/time fields:
      • Appointment date
      • Appointment time
    • Notes field:
      • Text field for call details
  • Configure access rights/security (briefly referenced).

B) Build menu and views for the model

  • Add a menu entry under the HMS/front desk area:
    • Create a menu (e.g., “appointments”)
    • Assign an action pointing to the model window action (e.g., action_hospital_appointment)
  • Create view XML (e.g., appointment_views.xml):
    • Tree view:
      • Show patient_id, meeting date, and notes behavior (notes shown in form; not necessarily in list)
    • Form view:
      • Include patient, date/time, notes, and (later) reference field
    • Ensure the action references the views and model correctly
  • Import the view XML into __manifest__.py.

C) Add sequence-backed reference field

  • Create sequence XML:
    • Create data/sequence.xml
    • Import into __manifest__.py
    • Define an ir.sequence entry with:
      • name and code
      • Prefix (example: HP)
      • Padding digits (example: 5)
      • Initial next number (example: 1)
  • Add a reference field to the model:
    • Field type: Char
    • Default placeholder (video uses "new")
  • Update views:
    • Show reference field
    • Set it read-only:
      • readonly="true"
  • Override create method:
    • Use @api.model_create_multi
    • For each record in vals_list:
      • If reference is empty or equals "new":
        • Assign:
          • self.env['ir.sequence'].next_by_code(<sequence_code>)
    • Return:
      • return super().create(vals_list)

D) Prevent sequence reset on updates using noupdate

  • Add noupdate="1" to XML data/sequence records:
    • Example approaches: put noupdate="1" on the record node or use a <data> wrapper with noupdate="1"
  • Test behavior:
    • Update the module and verify sequence settings are not overwritten
    • Toggle the noupdate behavior by removing/re-adding the sequence XML

Speakers / sources featured

  • Speaker: Unnamed presenter (“Good morning everyone… Today in this video…”)
  • Source references (non-speaking):
    • Odoo 17 source code: referenced via searching the source and locating create
    • Odoo built-in example apps:
      • Sales/Quotation behavior
      • Purchase order example for sequence
      • An example create override pattern
    • Odoo technical components:
      • ir.sequence
      • relation concepts similar to res.partner
      • XML data/sequences
      • @api.model_create_multi
      • super().create()

Original video