Organizing the addon module file structure

An addon module contains code files and other assets such as XML files and images. For most of these files, we are free to choose where to place them inside the module directory.

However, Odoo uses some conventions on the module structure, so it is advisable to follow them.

Getting ready

We are expected to have an addon module directory with only the __init__.py and __openerp__.py files.

How to do it…

To create the basic skeleton for the addon module:

  1. Create the directories for code files:
    $ cd path/to/my-module
    $ mkdir models
    $ touch models/__init__.py
    $ mkdir controllers
    $ touch controllers/__init__.py
    $ mkdir views
    $ mkdir security
    $ mkdir data
    $ mkdir demo
    $ mkdir i18n
    $ mkdir -p static/description
    
  2. Edit the module's top __init__.py file so that the code in subdirectories is loaded:
    # -*- coding: utf-8 -*-
    from . import models
    from . import controllers
    

This should get us started with a structure containing the most used directories, similar to this one:

.
├── __init__.py
├── 
__openerp__.py

├── controllers
│   └── __init__.py
├── data
├── i18n
├── models
│   └── __init__.py
├── security
├── static
│   └── description
└── 
views

How it works…

To provide some context, an Odoo addon module can have three types of file:

  • The Python code is loaded by the __init__.py files, where the .py files and code subdirectories are imported. Subdirectories containing code Python, in turn, need their own __init__.py
  • Data files that are to be declared in the data and demo keys of the __openerp__.py module manifest in order to be loaded. These are usually XML and CSV files for the user interface, fixture data, and demonstration data.
  • Web assets such as JavaScript code and libraries, CSS, and QWeb/HTML templates also play an important part. There are declared through an XML file extending the master templates to add these assets to the web client or website pages.

The addon files are to be organized in these directories:

  • models/ contains the backend code files, creating the Models and their business logic. A file per Model is recommended, with the same name as the model, for example, library_book.py for the library.book model. These are addressed in depth in Chapter 4, Application Models.
  • views/ contains the XML files for the user interface, with the actions, forms, lists, and so on. As with models, it is advised to have one file per model. Filenames for website templates are expected to end with the _template suffix. Backend Views are explained in Chapter 8, Backend Views, and website Views are addressed in Chapter 14, CMS Web Site Development.
  • data/ contains other data files with module initial data. Data files are explained in Chapter 9, Module Data.
  • demo/ contains data files with demonstration data, useful for tests, training or module evaluation.
  • i18n/ is where Odoo will look for the translation .pot and .po files. See Chapter 11, Internationalization, for more details. These files don't need to be mentioned in the manifest file.
  • security/ contains the data files defining access control lists, usually a ir.model.access.csv file, and possibly an XML file to define access Groups and Record Rules for row level security. See Chapter 10, Access Security, for more details on this.
  • controllers/ contains the code files for the website controllers, for modules providing that kind of feature. Web controllers are covered in Chapter 13, Web Server Development.
  • static/ is where all web assets are expected to be placed. Unlike other directories, this directory name is not just a convention, and only files inside it can be made available for the Odoo web pages. They don't need to be mentioned in the module manifest, but will have to be referred to in the web template. This is discussed in more detail in Chapter 14, CMS Website Development.

Tip

When adding new files to a module, don't forget to declare them either in the __openerp__.py (for data files) or __init__.py (for code files); otherwise, those files will be ignored and won't be loaded.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.143.65