odoo10 addon开发流程

odoo addon开发流程

创建一个addon(插件)

  • 命令如下
python odoo-bin scaffold  插件名   路径
# 例如
python odoo-bin scaffold hh_todo_app  myaddons
# 需要将myaddons的路径添加到odoo.conf配置文件的addons_path参数中去
  • 目录结构

image.png

开发To-do addon

  1. 创建huah_todo addon
python odoo-bin scaffold huah_todo extra_addons

创建huah_todo addon在extra_addons,需将extra_addons的绝对路径添加到debian/odoo.conf中的addons_path参数中

  1. 编写__manifest__.py文件
# -*- coding: utf-8 -*-
{
    'name': "huah_todo",# addons在应用栏中的展示名
		# 摘要
    'summary': """
        Short (1 phrase/line) summary of the module's purpose, used as
        subtitle on modules listing or apps.openerp.com""",
		# 描述
    'description': """
        Long description of module's purpose
    """,
		# 作者
    'author': "My Company",
  	# 网站
    'website': "http://www.yourcompany.com",

    # Categories can be used to filter modules in modules listing
    # Check https://github.com/odoo/odoo/blob/master/odoo/addons/base/module/module_data.xml
    # for the full list
    'category': 'Uncategorized',
    'version': '0.1',

    # any module necessary for this one to work correctly
  	# 依赖组件,当继承组件时需要编写,当按照组件时会自动安装依赖,卸载依赖组件时,该组件也会连带卸载
    'depends': ['base'],

    # always loaded
  	# 允许加载的xml文件
    'data': [
        # 'security/ir.model.access.csv',
        'views/views.xml',# 编写视图等xml文件
        'views/templates.xml',
    ],
    # only loaded in demonstration mode
    'demo': [
        'demo/demo.xml',
    ],
}
  1. 编写models/models.py
# -*- coding: utf-8 -*-
from odoo import models, fields, api
import datetime
class TaskInfo(models.Model):
    _name = 'huah_todo.taskinfo' # _name属性对应该模型在数据库中的表名
    _rec_name = 'work_name'      # _rec_name显示模型对象时指定显示的字段
    work_name = fields.Char(u'工作项目',required=True)  # 工作项目字段
    work_content = fields.Text(u'工作内容',required=True)   # 工作内容字段
    # 工作任务开始时间,设置readonly只读不可修改,默认为当前时间
    start_time = fields.Datetime(u'开始时间',default=lambda self:fields.Datetime.now(),
                                 readonly=True)
    # 结束时间,只读字段,当更改任务状态时才会被修改
    end_time = fields.Datetime(u'结束时间',readonly=True)
    # 累计用时,只读字段,任务完成时,计算时间差
    count_time =fields.Char(u'累计用时',readonly=True)
    # 任务状态,选择字段,默认未完成
    status = fields.Selection([(1,u'已完成'),(2,u'未完成'),(3,u'已取消')],
                              string=u'状态',default=2)
    # 当前进度,text字段
    current_process=fields.Text(u'当前进度')
    # 备注 
    remark = fields.Text(u'备注')
    
    @api.one   # 接收一个form对象传入self。
    def work_done(self):
        """
        前端按绑定work_done方法,改变任务状态并计算用时
        :return: 
        """
        self.status=1
        self.end_time=datetime.datetime.now()
        start_time=datetime.datetime.strptime(self.start_time,'%Y-%m-%d %H:%M:%S')
        end_time=datetime.datetime.strptime(self.end_time,'%Y-%m-%d %H:%M:%S')
        self.count_time=str((end_time-start_time).seconds//60)+'分钟'
        return True
  1. 编写views/views.xml

python odoo-bin scaffold命令创建的views.xml其实都注释了编写xml的示例

<odoo>
  <data>
    <!-- explicit list view definition -->
    <!--
    <record model="ir.ui.view" id="huah_todo.list">
      <field name="name">huah_todo list</field>
      <field name="model">huah_todo.huah_todo</field>
      <field name="arch" type="xml">
        <tree>
          <field name="name"/>
          <field name="value"/>
          <field name="value2"/>
        </tree>
      </field>
    </record>
    -->

    <!-- actions opening views on models -->
    <!--
    <record model="ir.actions.act_window" id="huah_todo.action_window">
      <field name="name">huah_todo window</field>
      <field name="res_model">huah_todo.huah_todo</field>
      <field name="view_mode">tree,form</field>
    </record>
    -->

    <!-- server action to the one above -->
    <!--
    <record model="ir.actions.server" id="huah_todo.action_server">
      <field name="name">huah_todo server</field>
      <field name="model_id" ref="model_huah_todo_huah_todo"/>
      <field name="code">
        action = {
          "type": "ir.actions.act_window",
          "view_mode": "tree,form",
          "res_model": self._name,
        }
      </field>
    </record>
    -->

    <!-- Top menu item -->
    <!--
    <menuitem name="huah_todo" id="huah_todo.menu_root"/>
    -->
    <!-- menu categories -->
    <!--
    <menuitem name="Menu 1" id="huah_todo.menu_1" parent="huah_todo.menu_root"/>
    <menuitem name="Menu 2" id="huah_todo.menu_2" parent="huah_todo.menu_root"/>
    -->
    <!-- actions -->
    <!--
    <menuitem name="List" id="huah_todo.menu_1_list" parent="huah_todo.menu_1"
              action="huah_todo.action_window"/>
    <menuitem name="Server to list" id="huah_todo" parent="huah_todo.menu_2"
              action="huah_todo.action_server"/>
    -->
  </data>
</odoo>
  • 编写菜单
<!-- Top menu item -->
<!--每日事项的一级按钮-->
<menuitem 
name="每日事项" #按钮的名字,用来显示在主菜单
id="huah_todo.menu_root" 	# 按钮唯一标示
action="huah_todo.taskinfo_window"/>	#指定要执行的窗口id
  • 编写视图窗口
<record model="ir.actions.act_window" id="huah_todo.taskinfo_window">
            <field name="name">每日事项</field>			# 窗口的名字
  					# 指定model,内容为model的_name属性
            <field name="res_model">huah_todo.taskinfo</field>
      			# 自动生成列表视图和form视图
            <field name="view_mode">tree,form</field>
</record>
  • 自定义tree视图
<record model="ir.ui.view" id="huah_todo.taskinfo_tree_view"> # id 视图的唯一标示
            <field name="name">taskinfo_tree_view</field>	# 视图名,可随意定义
            <field name="model">huah_todo.taskinfo</field># # 指定模型
            <field name="arch" type="xml">	# 声明类型为xml
                <tree> #tree标签定义要在tree视图显示的模型字段
                    <field name="work_name"/>
                    <field name="start_time"/>
                    <field name="end_time"/>
                    <field name="status"/>
                    <field name="current_process"/>
                </tree>
</record>

所有的视图都存在model="ir.ui.view"这张表里。

  • 自定义form视图
<record model="ir.ui.view" id="huah_todo.taskinfo">
    <field name="name">taskinfo_form_view</field>
    <field name="model">huah_todo.taskinfo</field>
    <field name="arch" type="xml">	# 前几行与tree视图类似
        <form># form标签用于创建数据时显示的字段		
            <header>	# header标签可定制按钮功能
                <button# button按钮的name属性绑定model的work_done方法
                        name="work_done"
                        type="object"
                        string="已完成"
                />
            </header>
            <sheet>	# sheet标签包含单元显示数据
                <group># group标签可将字段分组优化显示
                    <field name="work_name"/>
                    <field name="work_content"/>
                    <field name="current_process"/>
                    <field name="status"/>
                    <field name="remark"/>
                </group>
                <group>
                    <field name="start_time"/>
                    <field name="end_time"/>
                    <field name="count_time"/>
                </group>
            </sheet>
        </form>
    </field>
</record>

运行示例:
image.png
tree视图控制列表视图要显示的字段:
image.png
form视图控制创建数据时的显示:

image.png
点击已完成按钮会执行model的work_done方法修改结束时间和累计用时字段

image.png

posted @ 2019-04-02 09:20  Kingfan  阅读(584)  评论(0编辑  收藏  举报