1. self.env.cr.precommit.data.pop(f’mail.tracking.{self._name}’, {})
✅ What is it?
📌 Why is it used?
Odoo automatically tracks field changes for models with tracking=True. However, under certain conditions (e.g., during direct SQL operations or batch processes), this tracking cache can become stale or inconsistent, causing issues with mail tracking (e.g., mail.message logs showing incorrect or duplicate updates).
This line clears the cache for the model’s tracked values:
self.env.cr.precommit.data.pop(f’mail.tracking.{self._name}’, {})
- It avoids issues like:
- Incorrect change logs
- Redundant tracking messages
- Unwanted chatter messages on records
📌 Realistic Use Cases in Odoo
You may use it when performing manual write operations or mass updates to ensure clean tracking behavior:
2. self.env.flush_all() )
✅ What is it?
📌 Why is it used?
Odoo uses a lazy write system — some operations (e.g., computed fields or cache writes) are held in memory and written to the database only at specific points. flush_all() forces all pending changes to be committed to the DB immediately.
Syntax:
📌 Realistic Use Cases in Odoo
Use it when you need to:
- Access freshly written values in the same transaction
- Ensure all records are committed before continuing (especially for low-level operations or reports)
- The latest DB state is visible
- Chatter/field tracking remains accurate
- Performance remains stable
📌 Example
# Flush data to DB
self.env.flush_all()
# Prevent chatter noise
self.env.cr.precommit.data.pop(f‘mail.tracking.{self._name}’, {})
✅ Summary Table
| Code Snippet | Purpose | Use Case |
| flush_all() | Flush ORM cache to DB | Ensure consistent DB state before next op |
| precommit.data.pop(f’mail.tracking.{self._name}’, {}) | Clear mail tracking cache for current model | Prevent unwanted chatter messages |