Video summary
Odoo 17 Development: Sequence, No update Attribute, Rec Name and Create Method
Main summary
Key takeaways
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:
- Create a new Python model file (example:
appointment.py) by copying an existing model file (example:patient.py) and renaming it. - Import the new file in the module’s
__init__.py. - Define model fields including:
- A many-to-one relationship to patients
- Appointment date
- Appointment time (date/time field)
- Notes/call details (text field)
- 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
- Example:
- Configuration rule: specify the base model that provides selectable records
- Example:
hospital.patient
- Example:
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_nameso the appointment display name comes from a relevant field tied to the patient relation (e.g., showing the patient’s name frompatient_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:
- Create a sequence record in XML
- Add a new directory:
data - Create
sequence.xml - Import it in
__manifest__.py
- Add a new directory:
- Follow the Odoo example pattern for XML sequence definition.
- 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
- Add a new field to the appointment model:
- A
Charfield (example:reference) - Default placeholder initially set to
"new"
- A
- Make this reference visible in views:
- Add it to the form/tree view XML
- Set it read-only after auto-generation:
readonly="true"
- Auto-assign it by overriding
create:- Use
@api.model_create_multi - Override logic:
- Iterate through
vals_list - When
referenceis still"new"(or empty), assign:self.env['ir.sequence'].next_by_code(<sequence_code>)
- Then call:
return super().create(vals_list)
- Iterate through
- Use
- Debugging step:
- Print
vals_listto verify incoming values during record creation.
- Print
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
HPtoAPin 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 withnoupdate="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
- Copy an existing model (e.g.,
- Update
__init__.py:- Import the new
appointment.pymodule file
- Import the new
- Define model
hospital.appointment:- Inherit mail thread/related features (video references inheriting
mail.thread/ mail flow) - Set model description
- Inherit mail thread/related features (video references inheriting
- 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”
- Technical naming ends with
- Date/time fields:
- Appointment date
- Appointment time
- Notes field:
- Text field for call details
- Many-to-one:
- 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)
- Show
- Form view:
- Include patient, date/time, notes, and (later) reference field
- Ensure the action references the views and model correctly
- Tree view:
- 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.sequenceentry with:nameandcode- Prefix (example:
HP) - Padding digits (example: 5)
- Initial next number (example: 1)
- Create
- Add a reference field to the model:
- Field type:
Char - Default placeholder (video uses
"new")
- Field type:
- Update views:
- Show reference field
- Set it read-only:
readonly="true"
- Override
createmethod:- Use
@api.model_create_multi - For each record in
vals_list:- If
referenceis empty or equals"new":- Assign:
self.env['ir.sequence'].next_by_code(<sequence_code>)
- Assign:
- If
- Return:
return super().create(vals_list)
- Use
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 withnoupdate="1"
- Example approaches: put
- Test behavior:
- Update the module and verify sequence settings are not overwritten
- Toggle the
noupdatebehavior 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
createoverride pattern
- Odoo technical components:
ir.sequence- relation concepts similar to
res.partner - XML data/sequences
@api.model_create_multisuper().create()
- Odoo 17 source code: referenced via searching the source and locating