3.1 KiB
3.1 KiB
🚀 Odoo 19 Custom Module Development Guidelines
This document summarizes the coding standards, strict constraints, and Odoo 19 specific architectural changes discovered and enforced across the student_ERP_Metatron modules.
1. 🖥️ User Interface & Views
- Tree Views Deprecated: The
<tree>XML tag andtreeview mode have been completely replaced by<list>.- Action Window Definitions: You must define
view_modeaslist,forminstead oftree,form. - View Architectures: Use
<list>tags instead of<tree>when defining list view layouts.
- Action Window Definitions: You must define
2. 🗄️ Database & ORM Constraints
- SQL Constraints Deprecated: The
_sql_constraintslist attribute is no longer supported.- Odoo 19 Standard: Define SQL constraints using
models.Constraintat the class level. - Example:
_code_unique = models.Constraint('unique(code)', 'The Code must be unique!')
- Odoo 19 Standard: Define SQL constraints using
- Strict Validation: Models like
education.studenthave strict database-levelNotNullViolationconstraints for academic fields (batch_id,semester_id,section_id). These must always be populated when creating records, especially during automated testing.
3. 🔐 Security & Access Rights
- Group Fields: When assigning groups to
res.usersor defining access, note that the underlying model for groups usesres.groups.privilege. - Field Name Changes: When creating or writing to
res.users, the relational field for groups has been renamed fromgroups_idtogroup_ids. - Portal Controllers: Portal users lack access to standard internal models (like
ir.sequence). Sequence generation triggered from portal controllers (e.g., creating a library issue) must be executed using.sudo().
4. 🌐 Web Controllers & Translation
- Translation Function
_(): The standard translation function_()attempts to retrieve the user's language by accessingself.env.- The Issue: In HTTP controllers,
selfrefers to the Controller instance, not an ORM Model. Calling_("Error")inside a controller (e.g., within araise AccessError(...)) throwsAttributeError: 'NoneType' object has no attribute 'uid'. - The Solution: Avoid wrapping error strings in
_()within controllers unless explicitly usingrequest.env._().
- The Issue: In HTTP controllers,
5. 🧪 Automated Testing
- Buffered ORM Operations: Odoo 19 heavily buffers database inserts and updates.
- Testing Constraints: When testing database-level exceptions (like
UniqueViolation), the error will not trigger immediately upon calling.create(). You must callself.env.flush_all()within theassertRaisesblock to force the ORM to flush the transaction to PostgreSQL and trigger the exception. - Example:
with self.assertRaises(Exception): self.env['my.model'].create({'unique_field': 'duplicate'}) self.env.flush_all() # Crucial for triggering the DB error! - Constraint Exceptions: Odoo 19 often raises raw database exceptions (
Exception/psycopg2errors) rather than cleanly wrappedValidationErrorobjects for database-level violations. Adjust assertions accordingly.