Skip to content

Best Practices

This guide outlines the recommended practices for developing Odoo modules using the Cybrosys Assista PyCharm plugin. Following these guidelines will help you maintain high-quality, maintainable, and efficient code.

Module Structure

Directory Organization

module_name/
├── __init__.py
├── __manifest__.py
├── models/
│   ├── __init__.py
│   ├── models.py
│   └── other_models.py
├── views/
│   ├── views.xml
│   └── other_views.xml
├── security/
│   ├── ir.model.access.csv
│   └── security.xml
├── static/
│   ├── description/
│   │   └── icon.png
│   ├── src/
│   │   ├── js/
│   │   └── scss/
│   └── lib/
└── data/
    └── demo.xml

File Naming

  1. Python Files

    • Use lowercase with underscores
    • Use descriptive names
    • Group related models in separate files
    • Example: sale_order.py, purchase_order.py
  2. XML Files

    • Use descriptive names
    • Group by functionality
    • Example: sale_views.xml, purchase_views.xml
  3. Security Files

    • Use standard names
    • Keep access rights separate
    • Example: ir.model.access.csv, security.xml

Code Organization

Python Code

  1. Model Structure

    python
    # Imports
    from odoo import api, fields, models
    
    # Model Definition
    class MyModel(models.Model):
        _name = 'my.model'
        _description = 'My Model Description'
        _order = 'id desc'
    
        # Fields
        name = fields.Char(required=True)
        active = fields.Boolean(default=True)
    
        # Methods
        @api.model_create_multi
        def create(self, vals_list):
            return super().create(vals_list)
  2. Field Organization

    • Group related fields
    • Use clear, descriptive names
    • Add proper documentation
    • Set appropriate attributes
  3. Method Organization

    • Group by functionality
    • Use clear naming
    • Add docstrings
    • Follow Odoo conventions

XML Code

  1. View Structure

    xml
    <!-- View Definition -->
    <record id="view_form" model="ir.ui.view">
        <field name="name">my.model.form</field>
        <field name="model">my.model</field>
        <field name="arch" type="xml">
            <form>
                <!-- View Content -->
            </form>
        </field>
    </record>
  2. View Organization

    • Group related views
    • Use clear IDs
    • Add proper documentation
    • Follow naming conventions
  3. Inheritance

    • Use xpath expressions
    • Keep inheritance clean
    • Document modifications
    • Follow Odoo patterns

Development Workflow

Using the Plugin

  1. Module Creation

    • Use Module Template Generator
    • Choose appropriate template
    • Follow standard structure
    • Add proper documentation
  2. File Creation

    • Use File Creator
    • Choose correct template
    • Follow naming conventions
    • Add required content
  3. Code Generation

    • Use Code Snippets
    • Follow best practices
    • Customize as needed
    • Review generated code

Code Quality

  1. Inspections

    • Run regular checks
    • Address issues promptly
    • Follow suggestions
    • Maintain standards
  2. Documentation

    • Add docstrings
    • Document complex logic
    • Update README
    • Keep comments current
  3. Testing

    • Write unit tests
    • Test edge cases
    • Verify functionality
    • Document test cases

Performance

Database

  1. Queries

    • Use proper indexing
    • Optimize searches
    • Avoid N+1 queries
    • Use appropriate methods
  2. Records

    • Use sudo() carefully
    • Batch operations
    • Proper caching
    • Efficient updates

Views

  1. Form Views

    • Optimize field loading
    • Use proper widgets
    • Efficient layouts
    • Smart defaults
  2. List Views

    • Limit fields
    • Use proper filters
    • Optimize searches
    • Efficient grouping

Security

Access Rights

  1. Record Rules

    • Define proper rules
    • Use groups effectively
    • Document permissions
    • Test access control
  2. Field Access

    • Set proper attributes
    • Use groups
    • Document restrictions
    • Test permissions

Data Protection

  1. Sensitive Data

    • Encrypt when needed
    • Proper access control
    • Audit logging
    • Data validation
  2. User Input

    • Validate input
    • Sanitize data
    • Prevent injection
    • Handle errors

Maintenance

Code Updates

  1. Version Control

    • Use proper branching
    • Document changes
    • Review code
    • Test updates
  2. Dependencies

    • Manage versions
    • Update regularly
    • Document changes
    • Test compatibility

Documentation

  1. Code Documentation

    • Keep docstrings current
    • Document changes
    • Update comments
    • Maintain README
  2. User Documentation

    • Update user guides
    • Document features
    • Provide examples
    • Keep current

Next Steps