Skip to content

Smart Suggestions

The Smart Suggestions feature in the Odoo VSCode extension is designed to elevate your development workflow by providing intelligent, context-aware code completions. This feature not only accelerates coding but also helps maintain accuracy and consistency across your Odoo modules. Below, you will find a comprehensive and professional overview of how Smart Suggestions work, their technical underpinnings, and how they benefit both new and experienced Odoo developers.

Smart Navigation

  • Smart Template Navigation

    • Quickly jump to template definitions with t-call auto-suggestions
    • Navigate to any referenced template with a single click
    • Preview template content without leaving your current file
  • Intelligent Code Navigation

    • Field Definitions: Jump directly to field definitions with Ctrl+click or F12
    • Button Actions: Navigate from XML buttons to their corresponding Python methods
    • Menu Navigation: Quickly find and open parent menu definitions
    • Template References: Seamlessly navigate between template calls and their definitions
    • Inheritance Tracking: Follow inherit_id references to parent views
    • Record References: Resolve and navigate to ref records directly

Key Features

  • Context-Aware Completions: Suggestions adapt dynamically to your current editing context, whether you are working in Python or XML files.
  • Comprehensive Model and Field Discovery: Instantly access all models and fields defined in your workspace, including those introduced by custom modules.
  • Deep Relational Traversal: Effortlessly traverse through chains of relational fields, no matter how deeply nested.
  • Odoo-Specific Patterns: Recognizes and suggests completions for Odoo ORM, API decorators, XML view definitions, and more.
  • Automatic Updates: Suggestions are always in sync with your codebase, updating as you add, remove, or modify models and fields.
Smart Suggestions in action

How Smart Suggestions Work

The extension continuously scans your workspace for Python files that define Odoo models. It parses class definitions, extracts model names (from _name, _inherit, _inherits), and collects all field definitions, including their types and relational targets. This information is cached and updated in real time as you edit your code, ensuring that suggestions are always accurate and up-to-date.

When you trigger a completion (e.g., by typing or pressing Ctrl+Space), the extension analyzes your current context—such as the line you are editing, the surrounding code, and the file type—to determine the most relevant suggestions. For relational fields, it can follow chains of relationships, providing field suggestions for the final related model, no matter how many levels deep you traverse.

Actual Suggestion Contents and Triggers

Python Suggestions

Model Name Suggestions

  • Trigger Contexts:
    • Assigning to _inherit, _name, or _inherits (e.g., _inherit = "|")
    • Accessing models via self.env['|']
    • Defining relational fields: fields.Many2one("|"), fields.One2many("|"), fields.Many2many("|")
    • Setting comodel_name="|" in field definitions
  • Suggestions Provided:
    • All model names detected in your workspace, including those from custom modules and dependencies. Manifest Depends Suggestion:
    • Smart suggestions for module names in __manifest__.py
    • Auto-completes dependencies with proper module naming
    • Validates module existence to prevent typos

Field Name Suggestions

  • Trigger Contexts:
    • Inside API decorators: @api.depends('|'), @api.onchange('|'), @api.constrains('|')
    • When accessing fields on a record: self.some_field.|
    • In method definitions: def _compute_|, def _inverse_|, def _search_|
  • Suggestions Provided:
    • All field names for the current model, or for the related model if traversing through relational fields. Each suggestion includes the field type and, for relational fields, the related model. One2many Inverse Name Suggestion:
    • Intelligent suggestions for inverse_name in one2many fields
    • Analyzes model relationships to provide accurate inverse field names
    • Reduces manual lookup and potential errors`
  • @api.depends Field Suggestions
    • Smart field name completion within @api.depends decorators
    • Validates field existence in the current model
    • Suggests related fields with proper dot notation
  • @api.onchange Field Suggestions
    • Context-aware field name completion for @api.onchange
    • Suggests only relevant fields for the current model
    • Maintains consistency with field naming

Relational Field Traversal

  • How it Works:
    • The extension supports unlimited traversal through relational fields. For example, typing self.order_line.product_id.categ_id. will suggest all fields of the product.category model, following the chain of relationships from the current model.
    • This is invaluable for advanced Odoo development, where models often reference each other through complex relationships.

Inverse/Compute/Search Method Suggestions

  • Trigger Contexts:
    • When defining methods like def _compute_|, def _inverse_|, def _search_|
  • Suggestions Provided:
    • Field names of the current model, filtered as you type after the underscore.

XML Suggestions

Model Name Suggestions

  • Trigger Contexts:
    • In <field name="model">|</field>, <record model="|", and <field name="res_model">|</field>
  • Suggestions Provided:
    • All model names from your workspace, ensuring consistency between your Python and XML code.
  • Template Name Auto-Suggestion:
    • Instant suggestions for template names when using <t t-call="...">
    • Reduces errors and speeds up template referencing
    • Works across all template directories in your project
  • XPath Position Attribute Suggestion:
    • Context-aware suggestions for position attributes in XPath expressions
    • Supports all position types: before, after, inside, etc.
    • Validates XPath expressions in real-time

Field Name Suggestions

  • Trigger Contexts:
    • In <field name="|"> when the model context is known (e.g., inside a form view or after specifying <field name="model">)
  • Suggestions Provided:
    • All field names for the detected model, with type and related model (if any) in the suggestion details.

View Types, Widgets, and Classes

  • Trigger Contexts:
    • In <field name="view_mode">|</field>: Suggests view types like form, tree, kanban, etc.
    • In <field name="name" widget="|"/>: Suggests available Odoo widgets.
    • In <field name="name" class="|"/>: Suggests CSS classes found in your project.

How Deep Relational Traversal Works

  • The extension parses all models and fields in your workspace, including the type and related model for relational fields.
  • When you type a chain like self.a.b.c., it will:
    1. Identify the model of self (the current class).
    2. For each field in the chain, if it is a relational field, it will follow the related model.
    3. At the end of the chain, it will suggest all fields of the final model.
  • This works for any depth, as long as each field in the chain is a valid relational field.
  • Edge Case Handling: If a field in the chain is not a recognized relational field, suggestions will stop at that point, ensuring only valid completions are offered.

Example Scenarios

Python

python
class SaleOrder(models.Model):
    _name = 'sale.order'
    partner_id = fields.Many2one('res.partner')
    order_line = fields.One2many('sale.order.line', 'order_id')

class SaleOrderLine(models.Model):
    _name = 'sale.order.line'
    product_id = fields.Many2one('product.product')

# Typing:
self.order_line.product_id.  # Suggests all fields of 'product.product'

XML

xml
<record model="ir.ui.view" id="view_form">
    <field name="model">sale.order</field>
    <field name="arch" type="xml">
        <form>
            <field name="partner_id"/> <!-- Suggests all fields of 'sale.order' -->
        </form>
    </field>
</record>

Other Context-Aware Suggestions

  • API Decorators: Suggests field names for @api.depends, @api.onchange, @api.constrains.
  • Import Statements: Suggests common Odoo imports and module names for rapid development.
  • Action Definitions: Suggests model names for res_model in actions, reducing manual lookup.
  • View Modes: Suggests form, tree, kanban, etc., for view_mode fields in XML.
  • Widgets: Suggests available Odoo widgets in XML, helping you leverage advanced UI features.

Summary Table

Context (Python)What is Suggested
_inherit = ", env['All model names
fields.Many2one("All model names
comodel_name="All model names
@api.depends('All field names of current model
self.field.All fields of related model
self.a.b.c.Fields of final related model
def _compute_Field names of current model
Context (XML)What is Suggested
<field name="model">All model names
`<record model=""`
`<field name="">`
<field name="view_mode">View types (form, tree, etc.)
`<field name="name" widget=""`
`<field name="name" class=""`

These suggestions are always up-to-date with your workspace, and will update automatically as you add or change models and fields.

Benefits for All Developer Levels

For Beginners

  • Guided Development: Reduces the learning curve by surfacing available models, fields, and Odoo-specific patterns as you type.
  • Error Prevention: Minimizes typos and incorrect references, helping you write working code faster.

For Advanced Users

  • Productivity Boost: Accelerates development by reducing the need to manually look up model and field names.
  • Advanced Traversal: Supports complex model relationships and advanced Odoo patterns, making it ideal for large projects.
  • Consistency: Ensures that code references remain consistent across Python and XML files.

How Suggestions Stay Up-to-Date

  • The extension uses file system watchers to monitor changes in your Python files. When you add, remove, or modify models and fields, the internal index is refreshed automatically.
  • No manual refresh is needed—your suggestions are always current with your codebase.

Troubleshooting & Tips

  • Suggestions Not Appearing?
    • Ensure your workspace is open at the root of your Odoo project.
    • Check that your Python files are saved and free of syntax errors.
    • If you recently added new models or fields, try saving all files or reloading the VSCode window.
  • Performance: For very large codebases, initial indexing may take a few seconds. Suggestions will become more responsive as the index is built.
  • Custom Patterns: If you use non-standard model or field definitions, ensure they follow Odoo conventions for best results.

Tips for Effective Use

  1. Context Awareness

    • Suggestions adapt to your current context
    • Different suggestions for Python vs XML
    • Model-aware field suggestions
  2. Keyboard Shortcuts

    • Use Ctrl+Space to trigger suggestions
    • Tab to accept suggestions
    • Esc to dismiss suggestions

Next Steps