Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Odoo 12 Development Essentials

You're reading from   Odoo 12 Development Essentials Fast-track your Odoo development skills to build powerful business applications

Arrow left icon
Product type Paperback
Published in Dec 2018
Publisher Packt
ISBN-13 9781789532470
Length 404 pages
Edition 4th Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Daniel Reis Daniel Reis
Author Profile Icon Daniel Reis
Daniel Reis
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Quick Start Using the Developer Mode FREE CHAPTER 2. Preparing the Development Environment 3. Your First Odoo Application 4. Extending Modules 5. Import, Export, and Module Data 6. Models – Structuring the Application Data 7. Recordsets – Working with Model Data 8. Business Logic – Supporting Business Processes 9. External API – Integrating with Other Systems 10. Backend Views – Designing the User Interface 11. Kanban Views and Client-Side QWeb 12. Reports and Server-Side QWeb 13. Creating Website Frontend Features 14. Deploying and Maintaining Production Instances 15. Assessments 16. Other Books You May Enjoy

Modifying an existing model to add a field

Adding a custom field to an existing form is a common customization, and it can be done from the user interface, without the need to create a custom module.

For our to-do application, we want to select a group of people that will be able to collaborate on to-do items. We will identify them by setting a flag on their partner form. For that, we will add an Is Work Team? flag to the partner model.

The partner model is part of the Odoo core, and is available even if you haven't installed any apps yet. However, you may not have a menu option available to visit it. A simple way to add one is to install the Contacts application. Open the Apps top menu, look up this application, and install it, if you have not already done so:

After this, the Contacts application top menu option will be available.

Adding a field to a Model

To visit a Model's definition, with the developer mode enabled, in the Settings application go to the Technical | Database Structure | Models menu item.

Look up the Model having res.partner. The corresponding Model Description should be Contact. Click on it to open the form view, and you will see all the specific details about the partner model, including the field list:

Now, Edit the form and click on the Add a line button at the bottom of the Fields list. A pop-up window will be presented for new field creation.

Let's fill in the configuration:

  • Field Name: x_is_work_team
  • Field Label: Is Work Team?
  • Field Type: boolean

The Field Name must start with x_. This is mandatory for Models and Fields created directly through the user interface. Customizations made through add-on modules don't have this limitation.

That's it. Click save and close, and our new field should have been added to the Fields list. Chances are that this model has more than 80 fields, and so you might need to navigate to the next page of the Fields list to see it. To do so, use the right arrow in the upper-left corner of the Fields list.

Now, click on the upper-left save button to make this change permanent.

Adding a field to a form view

Our new field is now available in the partners model, but it is not yet visible to users. For that, we need to add it to the corresponding views.

Still on the Model having res.partner form, click on the Views tab, and we will be able to see all the view definitions for the model. As you can see, each view is a record in the database. Changing or adding view records is immediately effective, and will be visible the next time that view is reloaded:

There are a few important things to note in the Views list.

We can see that there are several View Type, such as Form, Tree, Search, or Kanban. The Search views are actually definitions of the filters available in the upper-right search box. The other view types are the different ways the data can be displayed. The basic ones are Tree, for list views, and Form, for the detailed form view.

Both Tree and List can be used to refer to the same view type. They are in fact lists, and the Tree name exists for historical reasons—in the past, list views used to have a tree hierarchical mode.

You will notice that the same view type can have several definitions. If you sort the list by View Type, that will be clear.

Each view type, such as Form, can have one or more base view definitions (the ones with an empty Inherited View field). Window Actions, used by menu items, can specify the particular base view to use. If none are defined, the one with the lowest Sequence is used. You can think of it as being the default view. Clicking on a views line, we will see a form with the view's details, including the Sequence value:

And each base view may have extensions, called Inherited Views. Each of these add incremental changes to the corresponding Base view, for example, adding a field to an existing form.

Extension views can themselves be extended by other extension views. In this case, the later extension is applied to the Base view after all preceding extensions have already been applied to it.

The res.partner model in particular can have a crowded view definitions list, since, like ours, many apps need to add extensions to it. An alternative is to access the particular view we want to extend, and edit it from there using the Developer Tools menu. This can also be used to learn what specific view is being used somewhere in the user interface.

Let's try it now:

  1. Click on the Contacts application to be presented with a list of contact cards, then click on any of the cards, we will navigate to the corresponding Form view:

  1. On the Form view, click on the Developer Tools menu (the bug icon in the upper-right corner) and select the Edit View: Form option. This will show the same view details form we saw before in Models, but positioned on the actual base view definition used here. As you can see, it is the res.partner.form view. We can see the owner module through the External ID field. In this case, it is base.view_partner_form, so we know that this view belongs to the base module. In the Architecture field, we can see the XML with the base view definition. We could simply edit the view architecture to add our new field, but in the long run it is not a good idea: this view is owned by an add-on module, and if some time in the future that module is upgraded, these customizations will be overwritten and lost. The proper way to modify a view is to create an Inherited View extension:

  1. Using the Inherited Views tab, we should now create an extension view to add elements to the Base view:
    1. First, we need to pick an element from the original view to use as the extension point. We can do that by inspecting the Architecture in Base view and choosing an XML element with a name attribute. Most of the time, this will be a <field> element. Here, we will pick the <field name="category_id" ...> element:


    1. Now, open the Developer Tools menu, click on the Edit View: Form option, select the Inherited Views tab, and click on Add a line at the bottom of the list.
    2. A pop-up window, Create Views which inherit from this one, will be shown, and we should fill it with the following values:
      • View Name: Contacts - Custom "Is Work Team" flag
      • Architecture: Use the following XML:
<field name="category_id" position="after">
<field name="x_is_work_team" />
</field>
      • The other important fields, such as the Model, View Type, and Inherited View, already have the correct default values
  1. We can now Save & Close, then, in the Edit View: Form window, click Save, and finally close it.

We will be able to see the change done once we reload the Contacts form view. This means reloading the page in your web browser. In most browsers, you can do that by pressing F5.

If we now visit again a contact form, we should see the new field on the left-hand side, below the Tags field:

You have been reading a chapter from
Odoo 12 Development Essentials - Fourth Edition
Published in: Dec 2018
Publisher: Packt
ISBN-13: 9781789532470
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.
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 $19.99/month. Cancel anytime
Banner background image