### 模块结构
每个模块都是一个目录内的一个*模块目录*。指定的模块目录使用 [`--addons-path`](https://www.odoo.com/documentation/9.0/reference/cmdline.html#cmdoption-odoo.py--addons-path) 选项.
Tip
大多数命令行选项也可以使用 [配置文件](https://www.odoo.com/documentation/9.0/reference/cmdline.html#reference-cmdline-config)
一个声明的Odoo模块 [manifest](https://www.odoo.com/documentation/9.0/reference/module.html#reference-module-manifest). 查看这个 [manifest documentation](https://www.odoo.com/documentation/9.0/reference/module.html#reference-module-manifest) 关于它的信息.
模块也是一个[Python package](http://docs.python.org/2/tutorial/modules.html#packages)在`__init__.py`文件, 包含在模块的各种Python文件导入指令.
例如,如果该模块有一个单一的 `mymodule.py` 文件 `__init__.py` 可能包含:
~~~ Python
from . import mymodule
~~~
Odoo提供了一种机制来帮助建立一个新的模块, [odoo.py](https://www.odoo.com/documentation/9.0/reference/cmdline.html#reference-cmdline-server) 有一个命令 [scaffold](https://www.odoo.com/documentation/9.0/reference/cmdline.html#reference-cmdline-scaffold) 创建一个空模块:
~~~ cmd
$ odoo.py scaffold <module name> <where to put it>
~~~
该命令创建一个模块的子目录,并自动创建一个模块一堆标准文件。他们中的大多数只包含注释代码或XML。大多数这些文件的使用将在本教程中解释。
>练习
模块创建
使用命令行上创建一个空的模块开院,并安装在Odoo。
1. 调用命令 ` odoo.py支架openacademy插件`.
2. 将清单文件调整到模块.
3. 不要打扰其他文件.
*openacademy/__openerp__.py*
~~~ Python
# -*- coding: utf-8 -*-
{
'name': "Open Academy",
'summary': """Manage trainings""",
'description': """
Open Academy module for managing trainings:
- training courses
- training sessions
- attendees registration
""",
'author': "My Company",
'website': "http://www.yourcompany.com",
# 类别可用于模块列表中的筛选器模块
# 检查 https://github.com/odoo/odoo/blob/master/openerp/addons/base/module/module_data.xml
# 为完整列表
'category': 'Test',
'version': '0.1',
# 这一个需要正确工作的模块
'depends': ['base'],
# 总是加载
'data': [
# 'security/ir.model.access.csv',
'templates.xml',
],
# 只有在示范模式中加载
'demo': [
'demo.xml',
],
}
~~~
*openacademy/__init__.py*
~~~ Python
# -*- coding: utf-8 -*-
from . import controllers
from . import models
~~~
*openacademy/controllers.py*
~~~ Python
# -*- coding: utf-8 -*-
from openerp import http
# class Openacademy(http.Controller):
# @http.route('/openacademy/openacademy/', auth='public')
# def index(self, **kw):
# return "Hello, world"
# @http.route('/openacademy/openacademy/objects/', auth='public')
# def list(self, **kw):
# return http.request.render('openacademy.listing', {
# 'root': '/openacademy/openacademy',
# 'objects': http.request.env['openacademy.openacademy'].search([]),
# })
# @http.route('/openacademy/openacademy/objects/<model("openacademy.openacademy"):obj>/', auth='public')
# def object(self, obj, **kw):
# return http.request.render('openacademy.object', {
# 'object': obj
# })
~~~
*openacademy/demo.xml*
~~~xml
<openerp>
<data>
<!-- -->
<!-- <record id="object0" model="openacademy.openacademy"> -->
<!-- <field name="name">Object 0</field> -->
<!-- </record> -->
<!-- -->
<!-- <record id="object1" model="openacademy.openacademy"> -->
<!-- <field name="name">Object 1</field> -->
<!-- </record> -->
<!-- -->
<!-- <record id="object2" model="openacademy.openacademy"> -->
<!-- <field name="name">Object 2</field> -->
<!-- </record> -->
<!-- -->
<!-- <record id="object3" model="openacademy.openacademy"> -->
<!-- <field name="name">Object 3</field> -->
<!-- </record> -->
<!-- -->
<!-- <record id="object4" model="openacademy.openacademy"> -->
<!-- <field name="name">Object 4</field> -->
<!-- </record> -->
<!-- -->
</data>
</openerp>
~~~
*openacademy/models.py*
~~~Python
# -*- coding: utf-8 -*-
from openerp import models, fields, api
# class openacademy(models.Model):
# _name = 'openacademy.openacademy'
# name = fields.Char()
~~~
*openacademy/security/ir.model.access.csv*
~~~csv
id,name,model_id/id,group_id/id,perm_read,perm_write,perm_create,perm_unlink
access_openacademy_openacademy,openacademy.openacademy,model_openacademy_openacademy,,1,0,0,0
~~~
*openacademy/templates.xml*
~~~xml
<openerp>
<data>
<!-- <template id="listing"> -->
<!-- <ul> -->
<!-- <li t-foreach="objects" t-as="object"> -->
<!-- <a t-attf-href="{{ root }}/objects/{{ object.id }}"> -->
<!-- <t t-esc="object.display_name"/> -->
<!-- </a> -->
<!-- </li> -->
<!-- </ul> -->
<!-- </template> -->
<!-- <template id="object"> -->
<!-- <h1><t t-esc="object.display_name"/></h1> -->
<!-- <dl> -->
<!-- <t t-foreach="object._fields" t-as="field"> -->
<!-- <dt><t t-esc="field"/></dt> -->
<!-- <dd><t t-esc="object[field]"/></dd> -->
<!-- </t> -->
<!-- </dl> -->
<!-- </template> -->
</data>
</openerp>
~~~