[Excellent] Odoo 8.0 in-depth development tutorial-module development Basics
Build an Odoo Module
Business Object
The business object is declared as a Python class, which is automatically loaded by Odoo.
Data Files
In XML or CSV format, metadata (view or workflow), configuration data (module parameters), and demo data are declared.
Web Controller
Process the requests from the Web browser.
Static web Data
Images used by the Web, CSS or JavaScript files.
Module structure
An Odoo module is also a Python module, which is stored in a directory and contains a _ init _. py file for importing other Python modules.
from . import mymodule
Odoo. py provides a sub-command scaffold to easily create an empty module.
$ odoo.py scaffold
After the command is executed, a sub-directory is created and contains some basic files required by the Odoo module.
Exercise #1
Run./odoo. py scaffold openacademy addons and create a module named openacademy under the addons directory. The generated directory file structure is as follows.
openacademy├── __init__.py├── __openerp__.py├── controllers.py├── demo.xml├── models.py├── security│ └── ir.model.access.csv└── templates.xml
For the content of each file, see the file or view the original text, and modify several identification texts in _ openerp _. py.
Object link ing
The ORM layer is a key component of Odoo. It can avoid writing most SQL statements to improve scalability and security.
Business Objects are written using the Python class (Model) derived from the Model. The _ name attribute of this class defines the name of the Model in the Odoo system.
from openerp import modelsclass MinimalModel(models.Model): _name = 'test.model'
Field
The field defines what the model can store and where it is stored. The field is defined by attributes in the model class.
from openerp import models, fieldsclass LessMinimalModel(models.Model): _name = 'test.model2' name = fields.Char()
Common attributes
Similar to the model, fields can be set through parameter transmission:
name = field.Char(required=True)
Common attributes of a field include:
String (unicode, default: field's name)
The label of the field in UI (visible by users ).
Required (bool, default: False)
If True, the field can not be empty, it must either have a default value or always be given a value when creating a record.
Help (unicode, default :'')
Long-form, provides a help tooltip to users in the UI.
Index (bool, default: False)
Requests that Odoo create a database index on the column
Simple Field
Fields can be divided into two types: simple fields and relational fields. the former is an atomic value and is directly stored in the database table corresponding to the model. The latter is connected to other records (the same model or different models can be used ).
Boolean, Date, and Char are all simple fields.
Reserved Field
Odoo automatically creates and maintains some fields in the model. These fields are reserved. These fields do not need to be manually modified.
Id)
The unique identifier for a record in its model
Create_date (Datetime)
Creation date of the record
Create_uid (Many2one)
User who created the record
Write_date (Datetime)
Last modification date of the record
Write_uid (Many2one)
User who last modified the record
Special Field
By default, Odoo requires a modelNameField, used for display and search. This can also be achieved by setting _ rec_name.
Exercise #2
Define a new model in the openacademy ModuleCourse, Openacademy/models. py:
# -*- coding: utf-8 -*-from openerp import models, fields, apiclass Course(models.Model): _name = 'openacademy.course' name = fields.Char(string="Title", required=True) description = fields.Text()
Data Files
Odoo is a highly data-driven system. Although Python code is used to customize module behavior, many module data are set during loading, and some modules only add data to Odoo.
Define module data through a data file. For example, you can use Element Definition data, each Element to create or update a record in the database. The format is as follows:
{a value}
Model
Odoo model name.
Id
External ID (External Identifier), which can be referenced to a record (and does not need to know the database ID of the record ).
Element
The name attribute is used to determine the field name (such as description). The body of the element provides the field value.
The data file must be in the list of loaded configuration files in the module, that is, _ openerp __. py's 'data' list (loaded all) or 'Demo' list (loaded only when set to loading demo data.
Exercise #3
Create a data file to add data to Course, edit openacademy/demo. xml, and confirm that the file is in the 'Demo' list of _ openerp _. py.
Course 0
Course 0's descriptionCan have multiple lines
Course 1
Course 2
Course 2's description
Actions and menus
In Odoo, actions and menus are defined as data records in the database, which are generally defined by data files.
Actions can be triggered in three ways:
Click the menu item (the menu necklace receives a specific action) and click the button on the view (if the button is connected to the action) as the context action of the object.
UseDeclare an ir. ui. menu and connect it to an action. You can use the following code.
Ideas
idea.idea
tree,form
Note: The action must be defined before the menu connection, and the data files are executed sequentially during loading. Therefore, the action ID must exist in the database before it can be used.
Exercise #4
Define a new menu item to access the OpenAcademy course.
Create the openacademy/views/openacademy. xml file, and add actions and menus to the file.
Courses
openacademy.course
form
tree,form
Create the first course
Add the data file name to 'data' in _ openerp _. py '.
'data': [ # 'security/ir.model.access.csv', 'templates.xml', 'views/openacademy.xml', ],
After the module is updated, you can see the menu to see the effect.
Basic View
A view defines how model data is displayed. Each type of view represents a data visualization mode.
Basic view definition
A view is defined in the form of an ir. ui. view model data.
View. name
Object_name
Course. form
Openacademy. course
Session. form
Openacademy. session
Sessions
Openacademy. session
Form
Tree, form
Digits = (6, 2) determines the precision of the floating point number. 6 indicates the total number of digits (excluding the decimal point), and 2 indicates the number of digits after the decimal point. therefore, digits = (6, 2) has a maximum of four digits before the decimal point.