Back to Rules
Python

Python Odoo Cursor Rules: Best Practices for Models, Views, Controllers & Enterprise Development

Claude Directory November 29, 2025
0 copies 2 downloads

Unlock expert guidelines for Python Odoo development in Cursor AI. Master ORM models, XML views, controllers, security, error handling, and performance optimization with ready-to-use code examples.

Rule Content
## Core Principles for Odoo Development

Focus on modular, readable code using Odoo's ORM, API tools, and view inheritance. Adhere to PEP 8 standards and Odoo's conventions for sustainable enterprise apps. Separate concerns across models, views, controllers, data files, and security.

## Building Odoo Models

Inherit from `models.Model` and apply decorators like `@api.model` or `@api.depends` for efficient data handling.

```python
from odoo import models, fields, api

class SaleOrder(models.Model):
    _inherit = 'sale.order'

    custom_field = fields.Char(string='Custom Note')

    @api.depends('order_line.price_total')
    def _compute_total_discount(self):
        for record in self:
            record.total_discount = sum(line.discount for line in record.order_line)
```

## Customizing Views with XML

Extend existing views using `<xpath>` for minimal changes and better maintainability.

```xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="view_order_form_inherit" model="ir.ui.view">
        <field name="name">sale.order.form.inherit</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='order_line']" position="after">
                <field name="custom_field"/>
            </xpath>
        </field>
    </record>
</odoo>
```

## Developing Web Controllers

Use `@http.route` for API endpoints with JSON responses and input validation.

```python
from odoo import http
import json

class CustomController(http.Controller):
    @http.route('/api/custom/data', type='json', auth='public', methods=['POST'])
    def get_custom_data(self, **kwargs):
        try:
            data = http.request.jsonrequest
            # Process data
            return {'status': 'success', 'data': data}
        except Exception as e:
            return {'status': 'error', 'message': str(e)}
```

## Error Handling and Validation

Raise `ValidationError` for user feedback, use `@api.constrains` for integrity checks, and log issues.

```python
from odoo.exceptions import ValidationError
import logging
_logger = logging.getLogger(__name__)

@api.constrains('custom_field')
def _check_custom_field(self):
    for rec in self:
        if len(rec.custom_field) < 5:
            raise ValidationError('Field must be at least 5 characters.')

# In methods:
try:
    # logic
except Exception as e:
    _logger.error('Error: %s', e)
    raise UserError('Operation failed.')
```

## Security and Access Control

Define groups and rules in XML for robust permissions.

```xml
<!-- security/ir.model.access.csv -->
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_custom_sale,sale.order.custom,model_sale_order,base.group_user,1,1,1,0
```

## Performance Tips

Apply domains, contexts, and computed fields to minimize queries. Use cron for heavy tasks.

```python
# Efficient search
orders = self.env['sale.order'].search([('state', '=', 'draft')], limit=100)

# Cron example
class CronModel(models.Model):
    _inherit = 'ir.cron'

    @api.model
def _run_heavy_task(self):
        # Background processing
        pass
```

## Module Structure

Organize with `__manifest__.py`, `models/`, `views/`, `controllers/`, `data/`, and `security/` folders. Enable i18n with `_()` and test thoroughly.

Comments

More Rules

View all
AI/ML

GLM-4.7 Optimized Config & System Prompt Designer

Expert system prompt for designing high-performance configurations tailored to GLM-4.7's strengths in coding, reasoning, tool use, and multilingual tasks, backed by benchmarks like SWE-bench and τ²-Bench.

C
Community
AI/ML

GLM-4.7 Open-Source Coding Expert: Optimized System Prompt

Leverage GLM-4.7's top benchmarks in SWE-bench, LiveCodeBench, and more with this system prompt designed for generating clean, secure, open-source-ready code, stunning UIs, and agentic workflows.

C
Community
AI/ML

GLM-4.7 Optimized Coding Agent

This system prompt transforms an AI into GLM-4.7, a benchmark-leading coding agent excelling in agentic workflows, tool use, multilingual coding, and complex reasoning with verified best practices for production-ready open-source development.

C
Community
DevOps

Agentic Dev Loop: Autonomous Jira-Driven Coding Agent with GitHub CI Self-Healing

Ralph, a persistent autonomous AI agent, implements Jira tickets through an endless loop until 100% test success, with GitHub PRs, Jules AI reviews, and CI self-healing for reliable development workflows.

C
Claude Directory
AI/ML

Türk Hukuku Uzmanı AI Agent: Güvenilir Yasal Danışman System Prompt

Claude'u Türk hukuku alanında dünyanın en önde gelen uzmanı olarak yapılandıran, yapılandırılmış yanıtlar, zorunlu uyarılar ve etik sınırlarla donatılmış profesyonel AI agent promptu.

C
Community
Database

PostgreSQL Best Practices: Expert Subagent Guide

Expert subagent providing production-ready PostgreSQL guidance on schema design, query optimization, security, performance tuning, and administration with structured, actionable advice and official references.

C
Claude Directory