Controller inheritance is a powerful feature in Odoo that allows you to extend or modify existing functionality without touching the original code. This guide will walk you through the process with a real-world example.
Why Inherit Controllers?
When you need to add new routes or enhance existing ones from another module, controller inheritance is your go-to solution. It maintains modularity and keeps your customizations clean and maintainable.
Here's what you need to know about inheriting a controller in Odoo 19:
1. Import the Parent Controller
First, import the controller you want to inherit from:
from odoo.addons.<module_name>.controllers.<controller_file> import <ControllerClass>
For example:
from odoo.addons.iobss_delivery.controllers.main import website_rggcontroller
This imports the website_rggcontroller class from the parent module's controller file. The pattern follows Odoo's standard structure where controllers are located in the controllers folder within each module.
2. Create Your Inherited Class
Define your new controller class that inherits from the parent:
class SgeedeWebsiteRggControllerInherit(website_rggcontroller):
# Your custom routes and methods go here
That's it! Your new controller now has access to all the routes and methods from the parent controller.
Example: Adding a Heatmap Route
Let's examine a practical implementation that adds a delivery heatmap visualization:
@http.route('/dms/heatmap', type='http', auth='user', csrf=False)
def dms_heatmap(self, **post):
# Get or set the date
dat = (datetime.now() + timedelta(hours=8)).strftime('%Y-%m-%d')
if "date" in request.params:
dat = request.params['date']
# Get view parameter (all, ibos, or sga)
view = request.params.get('view', 'all')
# Retrieve data and build matrix
# ... (business logic here)
# Render template
html = template_list.render(data=matrix, timeslot=timeslot_data,
date=dat, current_view=view)
return html
Key Components Explained
1. Route Decorator
The @http.route() decorator defines your endpoint:
- Path: /dms/heatmap - The URL where your route will be accessible
- Type: 'http' - Returns standard HTTP responses (use 'json' for JSON responses)
- Auth: 'user' - Requires user authentication
- CSRF: False - Disables CSRF protection (use cautiously!)
2. Handling Request Parameters
Access URL parameters using request.params:
dat = request.params.get('date', default_value)
view = request.params.get('view', 'all')
Best Practices
- Descriptive Class Names: Use clear naming that indicates it's an inheritance (e.g., OriginalControllerInherit).
- Maintain Parent Functionality: Unless you're overriding a specific method, all parent routes remain accessible.
- Security Awareness: Always consider authentication and CSRF protection. Disable CSRF only when absolutely necessary.
- Code Organization: Keep your controllers organized by functionality and maintain clear separation between modules.
- Use Super() for Method Overriding: If you need to extend an existing method rather than add a new one, use super():
Conclusion
Inheriting controllers in Odoo 19 is straightforward:
- Import the parent controller
- Create your class that inherits from it
- Add your custom routes.
This approach keeps your code modular, maintainable, and upgrade-friendly.
The example we explored demonstrates how to add a complex heatmap visualization feature by inheriting from an existing delivery controller, showcasing the flexibility and power of Odoo's controller inheritance system.
