India

ZestyBeanz Technologies Pvt Ltd 4th Floor, Nila, Technopark Thiruvananthapuram,
India – 695581
Phone: +91 471 4063254
Fax : +91 471 2700171

   .

ZestyBeanz Technologies Pvt Ltd
2987, Manikkath Cross Road
Ravipuram, Kochi, India - 682016
Phone: +91 484 4063254

  UAE

Zesty Labs
Office # 2003, Millennium Plaza Building
Sheikh Zayed Rd, Dubai, UAE
Phone: +971 4333 2222​
Mobile: +971-50-5417245​

  Germany​

ZestyBeanz Technologies GmbH
Reuterstraße 1
90408 Nürnberg
Fon: +49 911 4801 444
Fax: +49 911 4801 445

MVC in Odoo

Contact Form


Davis Jose's picture

MVC in Odoo

The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view and the controller. Each of these components are built to handle specific development aspects of an application.

MVC is one of the most frequently used industry-standard web development framework to create scalable and extensible projects.

Odoo is built upon a Model-View-Controller (MVC) architecture. One of the primary goals of this architecture is to separate the visual display of the information from the business rules and management of the underlying data. MVC does not replace Web Forms. We can create our Odoo website pages by using the MVC framework. This is true for maintaining flexibility in viewing data.

The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller."

From View to Model : the model sends notification to the view when its data has been modified in order the view to redraw its content. The model doesn't need to know the inner workings of the view to perform this operation. However, the view needs to access the internal parts of the model.

From View to Controller : the reason why the view has limited access to the controller is because the dependencies from the view to the controller need to be minimal: the controller can be replaced at any moment

MVC Model in Odoo

In Odoo, we can apply this model-view-controller semantic with

model : The PostgreSQL tables.
view : views are defined in XML files in Odoo.
controller : The objects of Odoo.

In Building a website in odoo, MVC architecture is heavily used to route ,redirect the pages and  to display data from database.  Also fully developed ecommerce module is also developed based on the workflow of this architecture.

Steps for calling a webpage in odoo website

1. Creating the controller structure

Before you can create (and call) a webpage you need to make a controller. This controller will tell Odoo which URL links to which webpage. Open up your module and create a folder ‘controllers’ if you don’t have one yet. After creating this folder make an __init__ file and add the followig line of code in it
from . import example

1.1 Creating the controller
For loading a website page : www.hostname.com/example
Defining the route in the controller

from odoo import http
from odoo.http import request
 
class Example(http.Controller):
    @http.route('/example', type='http', auth='public', website=True)
    def  render_example_page(self):
        return http.request.render('module_name.template_id’, {})
In order to let an user navigate to a specific page you will need an @http.route. This @http.route will tell Odoo that we want to link the url ‘/example’ to a specific webpage. Inside the @http.route you will see four variables. Let me explain them one by one:

‘/example’: Because Odoo needs to know which URL links to what (XML) template you’ll need to provide the URL first. In this example it means that http://localhost/example would call this @http.route
type=’http’: This will let Odoo know that your page is running in HTTP.
auth=’public’: Tells Odoo who can view the page.
You can choose between ‘public’, ‘user’ and ‘none’. If you choose ‘public’ anybody can view the page. When you choose ‘user’ only logged in people can view this page and if  you choose ‘none’ there will be no facilities to access the database or any configuration. website=True: Tells Odoo that is has to link to a webpage and that it is not just a Python function.

1.2 Returning the controller function
Finally, you need to return the function. Because the function needs to know which XML record needs to be called we pass it along like this:
return http.request.render('module_name.template_id', {})

http.request.render will call the view renderer from the Odoo framework. within the brackets “()” you have to specify the module_name.page_name. In this case my module is named ‘create_webpage_demo’ and I will create a webpage with the technical name
‘page_name’.

2. Passing data to the webpage

In the controller we will need to use ‘http.request.env’ to fetch data from a model.
companies = http.request.env['res.company'].sudo().search([])

The website looks at user rights and access rights too you’d need to make sure the users have enough rights. We have  simply add “.sudo()” so that the database query is executed as if it was done by the super administrator user without any security problems. The final thing to do is to pass this data, that is in the variable companies, to the webpage.

@http.route('/companies, type='http', auth='public', website=True)
    def  render_example_page(self):
        companies = http.request.env['res.company'].sudo().search([])
        return http.request.render('module_name.template_id’, {‘companies’:companies})

Because of doing {‘companies’: companies} in the dictionary all data from all companies will be passed along to the webpage.

2.1 Creating the Detail View on Website Page
<template id="detail_page" name="Detail page" page="True">
      <t t-call="website.layout">
        <div class="oe_structure">
          <div class="container">
            <center><h3>Company detail page</h3></center>
            <t t-foreach="companies" t-as="company">
              <h4><span t-esc="company.name"/></h4>
              <table class="table-striped table">
                <tr>
                  <td>Phone:</td>
                  <td><span t-esc="company.phone"/></td>
                </tr>
                <tr>
                  <td>E-mail:</td>
                  <td><span t-esc="company.email"/></td>
                </tr>
                <tr>
                  <td>Address:</td>
                  <td>
                    <span t-esc="company.street"/> <span t-esc="company.street2"/><br/>
                    <span t-esc="company.city"/> <span t-esc="company.country_id.name"/>
                  </td>
                </tr>
              </table>
            </t>
          </div>
        </div>
      </t>
    </template>
The controller will call this record .so when you would save this and update your module everything will work and passed along the dictionary companies we can access all company data and display in the page.

Creating webpages and using controllers in Odoo is quite easy. As the Odoo framework has a lot of built in libraries (such as requests, less, bootstrap, font awesome, …) it has never been as easy to create webpages as in Odoo 10. As a result you can create beautiful  website pages with beautiful layouts and easily customizable.