Skip to Content

3 - Odoo Architecture: The MVC Pattern Explained

If you are new to Odoo development, understanding its architecture is critical. Odoo follows a modified Model-View-Controller (MVC) design pattern. This approach separates the data, the user interface, and the web routing logic, making your applications scalable and easy to maintain.


1. What is MVC in Odoo?

Unlike traditional web frameworks (like Laravel or Django), Odoo's MVC is slightly adapted because the Odoo framework itself handles a lot of the heavy lifting.

  • Model (The Data): Handles the database structure and business logic. Written in Python.

  • View (The Interface): Defines what the user sees in the backend or frontend. Written in XML.

  • Controller (The Web Router): Handles custom HTTP web requests, API endpoints, and website portal pages. Written in Python.


2. Models (The Data Layer)

In Odoo, you do not write raw SQL queries. Instead, you define Models using Python classes. The Odoo ORM (Object-Relational Mapping) automatically creates the PostgreSQL database tables and handles all the CRUD (Create, Read, Update, Delete) operations.

Here is an example of a simple Odoo 19 Model:

from odoo import models, fields, api

class LibraryBook(models.Model):
# _name defines the database table name: library_book
_name = 'library.book'
_description = 'Library Book'

# These fields become columns in the PostgreSQL database
name = fields.Char(string='Title', required=True)
author = fields.Char(string='Author')
date_published = fields.Date(string='Published Date')
is_available = fields.Boolean(string='Is Available?', default=True)

💡 Pro Tip: By inheriting from models.Model, Odoo automatically gives your table built-in fields like create_uid, create_date, write_uid, and write_date for system auditing.


3. Views (The Presentation Layer)

The View determines how the data from the Model is presented to the user. Odoo uses XML files to define forms, lists (trees), kanban boards, and dashboards.

Because of the strict MVC separation, you can change how a form looks without ever having to touch or rewrite the Python logic.

Here is an example of a standard Form View for our Library Book model:

<odoo>
<record id="view_library_book_form" model="ir.ui.view">
<field name="name">library.book.form</field>
<field name="model">library.book</field>
<field name="arch" type="xml">
<form string="Book">
<sheet>
<group>
<field name="name"/>
<field name="author"/>
<field name="date_published"/>
<field name="is_available"/>
</group>
</sheet>
</form>
</field>
</record>
</odoo>


4. Controllers (The Web Layer)

Here is where Odoo differs from classic MVC. You do not need a Controller for standard backend operations. When a user clicks "Save" on an internal backend form, the XML View talks directly to the Python Model via the built-in ORM.

Controllers in Odoo (http.Controller) are used exclusively when you need to route public HTTP traffic, create custom RESTful APIs, or build pages for the frontend eCommerce website.

Here is an example of a Controller used to display books on a public webpage:

from odoo import http
from odoo.http import request

class LibraryController(http.Controller):

# The @http.route decorator maps a public URL to this Python method
@http.route('/library/books', type='http', auth='public', website=True)
def list_books(self, **kwargs):
# 1. Fetch data from the Model
books = request.env['library.book'].sudo().search([('is_available', '=', True)])
# 2. Pass the data to a View (QWeb template) to render the HTML
return request.render('my_module.public_books_template', {
'books': books
})


5. Summary of the Controller Data Flow

If a user hits a custom route on your Odoo website, the MVC communication flows like this:

  1. A user navigates to the URL /library/books.

  2. The Controller catches the HTTP request.

  3. The Controller asks the Model for available books.

  4. The Model queries the database and hands the records back.

  5. The Controller passes those records to the View.

  6. The View renders the final interface and displays it to the user.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.