Skip to content

unclecatvn/agent-skills / skills/odoo-19.0

odoo-19

Verified

Odoo 19 development knowledge base with 18 specialized guides covering Actions (ir.actions.*, cron jobs, server actions), Controllers (HTTP routing, endpoints, auth types), Data files (XML/CSV records, shortcuts, noupdate), API Decorators (@api.depends, @api.constrains, @api.ondelete, @api.onchange, @api.model, @api.private), SQL Constraints (models.Constraint replacing _sql_constraints), Database Indexes (models.Index), Module development (manifest, wizards, reports), Field types (Char, Text, Monetary, relational fields), Manifest configuration (__manifest__.py, dependencies, asset bundles), Mixins (mail.thread, mail.activity.mixin, mail.alias.mixin, utm.mixin), ORM Model methods (search, CRUD, domain filters, recordsets, CamelCase model naming), Migration scripts (pre/post/end hooks, data migration), OWL frontend components (hooks, services, lifecycle), Performance optimization (N+1 prevention, batch ops, _read_group), QWeb Reports (PDF/HTML, paper formats, barcodes, t-out), Security/ACL (record rules, field permissions, privilege-based groups, @api.private), Testing (TransactionCase, HttpCase, mocking, query count assertions), Transactions (savepoints, UniqueViolation, serialization failures), Translations (i18n, PO files, translatable fields), XML Views (list/form/search, kanban card templates, xpath inheritance, QWeb templates). Use when writing, reviewing, or debugging any Odoo 19 Python or XML code, creating or modifying modules, fixing performance issues, or looking up Odoo 19 API patterns and best practices. Includes CSS/SCSS asset authoring and review for Odoo addons.

references/odoo-19-model-guide.md

View on GitHub
# Odoo 19 Model Guide

## Python and Odoo Conventions

- Order imports into standard/external libraries, `odoo` imports, then imports
  from Odoo addons; alphabetize within each group.
- Prefer readable built-ins, comprehensions, and recordset helpers such as
  `filtered`, `mapped`, and `sorted` over clever abstractions. Avoid custom
  generators and decorators in addon business code.
- Use singular dotted model names, CamelCase classes, and `snake_case`
  variables. Reserve `_id`/`_ids` for integer IDs rather than recordsets.
- Order declarations as private attributes, defaults, fields, compute/inverse/
  search methods, selection methods, constraints/onchanges, CRUD overrides,
  actions, then business methods.
- Make overrides extendable: call `super()`, do not assume a singleton without
  `ensure_one()`, and never mutate context directly; use `with_context()` for a
  derived environment.
- Context is immutable and propagated automatically. Prefix custom behavior
  keys with the module name; avoid generic `default_<field>` keys because they
  can leak into nested creates on unrelated models.

Guide for working with Odoo 19 ORM, recordsets, CRUD operations, and domain filters.

## Table of Contents

- [Models](#models)
- [Fields](#fields)
- [SQL Constraints](#sql-constraints)
- [Database Indexes](#database-indexes)
- [Recordsets](#recordsets)
- [CRUD Operations](#crud-operations)
- [Search Domains](#search-domains)
- [Environment](#environment)
- [SQL Execution](#sql-execution)
- [Inheritance](#inheritance)

---

## Models

### Defining a Model

> **Odoo 19 Change**: when `_name` is missing, Odoo derives it from the CamelCase class name (each capital letter → `.` separator, e.g. `ResPartner` → `res.partner`, `SaleOrder` → `sale.order`) and logs the warning `Class ... has no _name, please make it explicit`. Keep `_name` explicit on new models. A string `_inherit` without `_name` still sets `_name = _inherit`; a list `_inherit` does not (a one-item list meant `_inherit[0]` in Odoo 18), so `class Partner` with `_inherit = ['res.partner']` and no `_name` creates a new model `partner`.

```python
from odoo import models, fields

class MyModel(models.Model):
    _name = 'my.model'  # omitting it works (derived from the class name) but logs a warning
    _description = 'My Model'

    field1 = fields.Char()
    field2 = fields.Integer(string="Field Label")
```

```python
# When _name differs from class name convention, specify explicitly:
class CustomNameModel(models.Model):
    _name = 'custom.different.name'
    _description = 'Custom Named Model'

    name = fields.Char()
```

### Model Attributes

| Attribute       | Description                                                                   |
| --------------- | ----------------------------------------------------------------------------- |
| `_name`         | Model name (derived from the CamelCase class name with a warning if missing)  |
| `_description`  | Model description                                                             |
| `_order`        | Default sort order                                                            |
| `_rec_name`     | Field to use as name representation                                           |
| `_inherit`      | Model(s) to inherit from                                                      |
| `_inherits`     | Delegation inheritance                                                        |
| `_table`        | Database table name                                                           |
| `_log_access`   | Enable create_date, write_date, create_uid, write_uid                         |
| `_auto`         | Auto-create database table                                                    |
| `_abstract`     | Abstract model                                                                |
| `_transient`    | Transient model                                                               |
| `_parent_store` | Enable parent_path field                                                      |
| `_fold_name`    | Field for kanban fold                                                         |

### Model Types

| Class                   | Description                        |
| ----------------------- | ---------------------------------- |
| `models.Model`          | Regular database model             |
| `models.TransientModel` | Temporary/wizard model             |
| `models.AbstractModel`  | Abstract model (no database table) |

---

## Fields

### Field Definition

Fields are defined as class attributes on the model.

```python
from odoo import models, fields

class MyModel(models.Model):
    _name = 'my.model'

    name = fields.Char(required=True)
    description = fields.Text()
    active = fields.Boolean(default=True)
    count = fields.Integer()
    price = fields.Float(digits='Product Price')
```

### Default Values

```python
# Value
name = fields.Char(default="A value")

# Function
def _default_name(self):
    return self.get_value()

name = fields.Char(default=lambda self: self._default_name())
```

### Field Types

| Type              | Class                        | Description                 |
| ----------------- | ---------------------------- | --------------------------- |
| **Basic**         |                              |                             |
| Boolean           | `fields.Boolean()`           | True/False                  |
| Char              | `fields.Char()`              | String (limited length)     |
| Float             | `fields.Float()`             | Floating-point number       |
| Integer           | `fields.Integer()`           | Integer                     |
| **Advanced**      |                              |                             |
| Binary            | `fields.Binary()`            | Binary data (files)         |
| Html              | `fields.Html()`              | HTML content                |
| Image             | `fields.Image()`             | Image (enhanced Binary)     |
| Monetary          | `fields.Monetary()`          | Monetary amount             |
| Selection         | `fields.Selection()`         | Selection from list         |
| Text              | `fields.Text()`              | Long text                   |
| **Date**          |                              |                             |
| Date              | `fields.Date()`              | Date (no time)              |
| Datetime          | `fields.Datetime()`          | Date and time               |
| **Relational**    |                              |                             |
| Many2one          | `fields.Many2one()`          | Many-to-one                 |
| One2many          | `fields.One2many()`          | One-to-many                 |
| Many2many         | `fields.Many2many()`         | Many-to-many                |
| **Pseudo**        |                              |                             |
| Reference         | `fields.Reference()`         | Reference to any model      |
| Many2oneReference | `fields.Many2oneReference()` | Many2one with dynamic model |

### Computed Fields

```python
from odoo import api

total = fields.Float(compute='_compute_total', store=True)

@api.depends('value', 'tax')
def _compute_total(self):
    for record in self:
        record.total = record.value + record.value * record.tax
```

### Related Fields

```python
nickname = fields.Char(related='partner_id.name', store=True)
```

### Automatic Fields

| Field          | Type     | Description           |
| -------------- | -------- | --------------------- |
| `id`           | int      | Identifier            |
| `display_name` | char     | Display name          |
| `create_date`  | datetime | Creation timestamp    |
| `create_uid`   | Many2one | Creator               |
| `write_date`   | datetime | Last update timestamp |
| `write_uid`    | Many2one | Last modifier         |

### Reserved Field Names

| Name          | Type      | Purpose                   |
| ------------- | --------- | ------------------------- |
| `name`        | Char      | Default `rec_name`        |
| `active`      | Boolean   | Toggles global visibility |
| `state`       | Selection | Lifecycle stages          |
| `parent_id`   | Many2one  | Tree structure parent     |
| `parent_path` | Char      | Tree structure path       |
| `company_id`  | Many2one  | Multi-company field       |

---

## SQL Constraints

> **Odoo 19 Breaking Change**: `_sql_constraints` is **no longer supported**. Use `models.Constraint` instead.

### models.Constraint (Odoo 19)

SQL constraints are now defined as model attributes using `models.Constraint`:

```python
from odoo import models, fields

class MyModel(models.Model):
    _name = 'my.model'
    _description = 'My Model'

    name = fields.Char(required=True)
    code = fields.Char()
    quantity = fields.Integer()

    # UNIQUE constraint
    _unique_name = models.Constraint(
        'UNIQUE(name)',
        'Name must be unique!',
    )

    # UNIQUE on multiple fields
    _unique_name_code = models.Constraint(
        'UNIQUE(name, code)',
        'The combination of name and code must be unique!',
    )

    # CHECK constraint
    _check_quantity = models.Constraint(
        'CHECK(quantity > 0)',
        'Quantity must be positive!',
    )
```

### Constraint Naming

The attribute name must start with `_`; the SQL name is `<table>_<attribute name without the leading _>`
(`_unique_name` on `my_model` → `my_model_unique_name`). There is no separate name argument, and
`_sql_constraints` is ignored in 19 (warning only). For a unique index with a message, use
`models.UniqueIndex('(name)', 'Name must be unique!')`.

### Migration from `_sql_constraints`

```python
# ❌ OLD (Odoo 18 and earlier) — NO LONGER WORKS in Odoo 19
class MyModel(models.Model):
    _name = 'my.model'
    _sql_constraints = [
        ('name_uniq', 'UNIQUE(name)', 'Name must be unique!'),
        ('check_qty', 'CHECK(quantity > 0)', 'Quantity must be positive!'),
    ]

# ✅ NEW (Odoo 19)
class MyModel(models.Model):
    _name = 'my.model'

    _name_uniq = models.Constraint(
        'UNIQUE(name)',
        'Name must be unique!',
    )

    _check_qty = models.Constraint(
        'CHECK(quantity > 0)',
        'Quantity must be positive!',
    )
```

### Overriding Constraints in Inherited Models

Constraints can be overridden or removed in inherited models:

```python
class ExtendedModel(models.Model):
    _inherit = 'my.model'

    # Override constraint with different check
    _check_qty = models.Constraint(
        'CHECK(quantity >= 0)',
        'Quantity cannot be negative!',
    )
```

---

## Database Indexes

### Field-Level Index

```python
name = fields.Char(index=True)  # Simple btree index
```

### Declarative Index (Odoo 19)

For composite or custom indexes, use `models.Index`:

```python
class MyModel(models.Model):
    _name = 'my.model'

    name = fields.Char()
    code = fields.Char()
    date = fields.Date()

    # Composite index on multiple fields
    _name_code_idx = models.Index('(name, code)')

    # Index with specific method
    _date_idx = models.Index('(date DESC)')
```

> **Warning**: Don't over-index — indexes consume space and impact INSERT/UPDATE/DELETE performance.

---

## Recordsets

### Active Record Interface

```python
# Read field
record.name
record.company_id.name

# Write field
record.name = "Bob"

# Dynamic field access
field = "name"
record[field]
```

### Iteration

```python
def do_operation(self):
    for record in self:
        # record is a single record
        print(record.name)
```

### Record Cache and Prefetching

Odoo maintains a cache and prefetches records/fields following heuristics.

```python
# Without prefetching: 2000 queries
for partner in partners:
    print(partner.name)
    print(partner.lang)

# With prefetching: 1 query
for partner in partners:
    print(partner.name)
    print(partner.lang)
```

---

## CRUD Operations

### Create

```python
# Single record
record = self.env['model.name'].create({'field': 'value'})

# Multiple records (batch)
records = self.env['model.name'].create([
    {'field': 'value1'},
    {'field': 'value2'},
])
```

### Read

```python
# Browse
record = self.env['model.name'].browse(record_id)
records = self.env['model.name'].browse([id1, id2, id3])

# Read
data = records.read(['field1', 'field2'])
```

### Write

```python
# Single record
record.write({'field': 'value'})

# Multiple records
records.write({'field': 'value'})
```

### Unlink (Delete)

```python
# Single record
record.unlink()

# Multiple records
records.unlink()
```

---

## Search Domains

A search domain is a first-order logical predicate for filtering.

### Domain Condition

```python
# Simple condition
domain = [('name', '=', 'ABC')]

# Multiple conditions
domain = [('name', '=', 'ABC'), ('phone', 'like', '7620')]
```

### Operators

| Operator                             | Description        |
| ------------------------------------ | ------------------ |
| `=`                                  | equals             |
| `!=`                                 | not equals         |
| `>`, `>=`, `<`, `<=`                 | comparison         |
| `=?`                                 | unset or equals    |
| `=like`, `like`, `ilike`, `=ilike`   | pattern matching   |
| `in`, `not in`                       | in list            |
| `child_of`, `parent_of`              | tree traversal     |
| `any`, `any!`, `not any`, `not any!` | relation traversal |

### Logical Operators

```python
# AND (implicit)
domain = [('name', '=', 'ABC'), ('state', '=', 'draft')]

# OR
domain = '|', [('name', '=', 'ABC')], [('name', '=', 'XYZ')]

# NOT
domain = '!', [('state', '=', 'draft')]
```

### Domain Class

```python
from odoo.fields import Domain

# Create domain
d1 = Domain('name', '=', 'abc')
d2 = Domain('phone', 'like', '7620')

# Combine
d3 = d1 & d2  # AND
d4 = d1 | d2  # OR
d5 = ~d1      # NOT

# Parse from list
domain = Domain([('name', '=', 'abc'), ('phone', 'like', '7620')])

# Serialize to list
domain_list = list(domain)
```

### Search Methods

```python
# Search
records = self.env['model'].search(domain)

# Search with limit
records = self.env['model'].search(domain, limit=10)

# Search with offset
records = self.env['model'].search(domain, offset=20)

# Search with order
records = self.env['model'].search(domain, order='name ASC')

# Search count
count = self.env['model'].search_count(domain)

# Search and read
records = self.env['model'].search_read(domain, ['field1', 'field2'])

# Search fetch (Odoo 19+)
records = self.env['model'].search_fetch(domain, ['field1', 'field2'])

# Name search
records = self.env['model'].name_search('keyword', operator='ilike')
```

---

## Environment

The environment holds:

- Database cursor (`cr`)
- Current user (`user`, `uid`)
- Context (`context`)
- Record cache

### Accessing Environment

```python
# From recordset
env = record.env

# Create new recordset in another model
model = env['another.model']

# Access properties
env.uid         # Current user id
env.user        # Current user recordset
env.company     # Current company
env.companies   # Allowed companies
env.lang        # Current language
```

### Altering Environment

Each of these returns a **new** recordset bound to the modified environment; the
original recordset is unchanged. Always use the return value (assign it or chain
the next call on it).

```python
# Change context
records_fr = records.with_context(lang='fr_FR')

# Change user
records_as_user = records.with_user(user_id)

# Change company
records_for_company = records.with_company(company_id)

# Change environment completely
records_new_env = records.with_env(new_env)

# Sudo (superuser mode)
records_sudo = records.sudo()
```

---

## SQL Execution

### Raw SQL

```python
# Execute query
self.env.cr.execute("SELECT id FROM table WHERE field = %s", (value,))

# Fetch results
results = self.env.cr.fetchall()
row = self.env.cr.fetchone()
```

### SQL Class (Recommended)

```python
from odoo.tools import SQL

# Build query
query = SQL("SELECT id FROM table WHERE field = %s", value)

# Execute
self.env.cr.execute(query)
```

### Flush and Invalidate

Before SQL queries, flush pending data:

```python
# Flush all records of a model
self.env['model'].flush_model(['field1', 'field2'])

# Flush specific recordset
records.flush_recordset(['field1', 'field2'])
```

After SQL modifications, invalidate cache:

```python
# Invalidate all records of a model
self.env['model'].invalidate_model(['field1', 'field2'])

# Invalidate specific recordset
records.invalidate_recordset(['field1', 'field2'])

# Notify field modification
records.modified(['field1', 'field2'])
```

---

## Inheritance

### Classical Inheritance

Create new model from existing one:

```python
class Inheritance1(models.Model):
    _name = 'inheritance.1'
    _description = 'Inheritance One'

    name = fields.Char()

class Inheritance2(models.Model):
    _name = 'inheritance.2'
    _inherit = ['inheritance.1']
    _description = 'Inheritance Two'

    # Inherits name field from inheritance.1
    # Adds new fields/methods
```

### Extension

Extend existing model in-place:

```python
class Extension0(models.Model):
    _name = 'extension.0'
    _description = 'Extension zero'

    name = fields.Char(default="A")

class Extension0(models.Model):
    _inherit = 'extension.0'

    description = fields.Char(default="Extended")
```

### Delegation

Delegate fields to child records:

```python
class Screen(models.Model):
    _name = 'delegation.screen'

    size = fields.Float(string='Screen Size')

class Laptop(models.Model):
    _name = 'delegation.laptop'

    _inherits = {
        'delegation.screen': 'screen_id',
    }

    name = fields.Char(string='Name')
    screen_id = fields.Many2one('delegation.screen', required=True, ondelete="cascade")

# Can access size directly on laptop
laptop.size
```

---

## References

- Source: Odoo 19 documentation `/doc/developer/reference/backend/orm.rst`
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall