Reader small image

You're reading from  Odoo 15 Development Essentials - Fifth Edition

Product typeBook
Published inFeb 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781800200067
Edition5th Edition
Languages
Tools
Right arrow
Author (1)
Daniel Reis
Daniel Reis
author image
Daniel Reis

Daniel Reis has a degree in applied mathematics and an MBA. He has had a long career in the IT industry, mostly as a consultant implementing business applications in a variety of sectors. He has been working with Odoo (OpenERP at the time) since 2010 and is an active contributor to the Odoo Community Association (OCA), where he also serves as a board member. He is the managing director of Open Source Integrators, a leading open source and Odoo consultancy firm.
Read more about Daniel Reis

Right arrow

Chapter 4: Extending Modules

One of Odoo's most powerful capabilities is being able to add features without directly touching the code of the extended modules. This allows for clean feature extensions that are isolated in their own code components. Extending modules can be achieved through inheritance mechanisms, which work as modification layers on top of existing objects. These modifications can happen at every level – including the model, view, and business logic levels. Instead of directly modifying an existing module, we will create a new module by adding a layer on top of the existing one with the intended modifications.

The previous chapter guided us through creating a new app from scratch. In this chapter, we will learn how to create modules that extend existing apps or modules and use existing core or community features.

To achieve this, we will cover the following topics:

  • Learning project – extending the Library app
  • Adding a new field...

Technical requirements

For this chapter, you will need an Odoo server that you can command from a terminal session.

The code in this chapter depends on the code that we created in Chapter 3, Your First Odoo Application. You should have that code in your add-ons path and have a database with the library_app module installed.

This chapter adds the library_member add-on module to our project. The corresponding code can be found in this book's GitHub repository, https://github.com/PacktPublishing/Odoo-15-Development-Essentials, in the ch04 directory.

Learning project – extending the Library app

In Chapter 3, Your First Odoo Application, we created the initial module for the Library app and provided a book catalog. Now, we will extend the application to add library members and allow them to borrow books. For this, we will create an extension module called library_member.

These are the features we must provide:

  • Library books can be available to be borrowed or not. This information should be shown in the book form and on the website's catalog page.
  • Some library member master data, along with the library card number, plus personal data, such as name, address, and email.
  • We would like to provide members with the messaging and social features that are available on the borrowing form, including the planned activities widget, to allow for better collaboration.

Later, we plan to introduce a feature that allows members to borrow books from the library, but this is outside our scope for now. This will...

Adding a new field to an existing model

Our first task is to add the is_available Boolean field to the book model. For now, this will be a simple editable field, but at a later stage, we can imagine changing it to be automatic, based on books that have been borrowed and returned.

To extend an existing model, we must use a Python class with the _inherit attribute, identifying the model being extended. The new class inherits all of the features of the parent Odoo model, and we only need to declare the modifications to introduce. We can think of this type of inheritance as getting a reference for the existing model and making in-place changes to it.

Adding new fields with the in-place model extension

Extending models is done through Python classes by using the Odoo-specific inheritance mechanism that's declared using the _inherit class attribute. This _inherit class attribute identifies the model to be extended. The declared calls capture all the features of the inherited...

Extending models using classic in-place extension

We can think of the classic model inheritance as an in-place extension. When a Python class with the _inherit attribute is declared, it gets a reference to the corresponding model definition, to then add extensions to it. The model definition is stored in the Odoo model registry and is available for us to add further modifications to it.

Now, let's learn how to use this for frequent extension use cases: modifying the attributes of an existing field and extending Python methods to add or modify business logic.

Incrementally modifying existing fields

When we're extending a model, existing fields can be modified incrementally. This means that we only need to define the field attributes to change or add.

We will make two changes to the book fields that were created in the library_app module:

  • On the isbn field, add a help tooltip explaining that we support both 10- and 13-digit ISBNs, with the latter being implemented...

More model inheritance mechanisms

The previous section discussed classic inheritance, which can be seen as an in-place extension. This is the most frequently used approach, but the Odoo framework also supports a few other extension mechanisms that are useful in other cases.

These are delegation inheritance, prototype inheritance, and the use of mixins:

  • Delegation inheritance embeds another model in the inheriting one. For example, a User record embeds a Partner record, so that a User record has all the fields available for the Partner records, plus the fields specific to the User records. It is used through the _inherits attribute.
  • Prototype inheritance creates a new model by copying the features from the inherited model and has a database table and data. It is not used often and it is never used in the Odoo-included add-on modules. It is used to set _inherit with the model to copy and the _name attribute with the identifier for the new model to be created.
  • Mixin...

Extending views and data

Views and other data components can also be modified by an extension module. For views, the case is usually to add features. The view presentation structure is defined with XML. To extend this XML, we must locate the node to extend and then declare the action to perform there, such as inserting additional XML elements.

The other data elements represent records that were written to the database. Extension modules can write on them to change some values.

Extending views

Views are defined using XML and are stored in the architecture field, arch. To extend a view, we must locate the node where the extension will take place, and then perform the intended change, such as adding XML elements.

Odoo provides a simplified notation to extend XML by using the XML tag we want to match –  <field>, for example – with one or more distinctive attributes to match, such as name. Then, we must add the position attribute to declare the...

Extending web pages

Extensibility is a key design choice for the Odoo framework, and the Odoo web components are no exception. So, Odoo web controllers and templates can be also extended.

The Library app that we created in the previous Chapter 3, Your First Odoo Application, provided a book catalog page that now needs to be improved.

We will extend it to leverage the book availability information that was added by the Library Members module:

  • On the controller side, we will add support to a query string parameter to filter only the available books; that is, /library/books?available=1.
  • On the template side, we will specify the books that are not available.

Let's start extending the web controller.

Extending the web controllers

Web controllers are responsible for handling web requests and rendering the page to return as a response. They should focus on presentation logic, not deal with business logic, which should be incorporated into model methods...

Summary

Extensibility is a key feature of the Odoo framework. We can build add-on modules that change or add features to other existing add-ons at the several layers needed to implement features in Odoo. With this, our projects will be able to reuse and extend third-party add-on modules in a clean and modular way.

At the model layer, we use the _inherit model attribute to get a reference to an existing model and then make in-place modifications to it. The field objects inside the model also support incremental definitions so that we can redeclare an existing field, providing only the attributes to change.

Additional model inheritance mechanisms allow you to reuse data structures and business logic. Delegation inheritance, which is activated with the delegate=True attribute on a many-to-one relationship field (or the old-style inherits model attribute), makes all the fields from the related model available and reuses its data structure. Prototype inheritance, which uses _inherit...

Further reading

The following are some additional references to the official documentation, which can provide useful information regarding module extensions and inheritance mechanisms:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Odoo 15 Development Essentials - Fifth Edition
Published in: Feb 2022Publisher: PacktISBN-13: 9781800200067
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Daniel Reis

Daniel Reis has a degree in applied mathematics and an MBA. He has had a long career in the IT industry, mostly as a consultant implementing business applications in a variety of sectors. He has been working with Odoo (OpenERP at the time) since 2010 and is an active contributor to the Odoo Community Association (OCA), where he also serves as a board member. He is the managing director of Open Source Integrators, a leading open source and Odoo consultancy firm.
Read more about Daniel Reis