python odoo-bin scaffold 插件名 路径 # 例如 python odoo-bin scaffold hh_todo_app myaddons # 须要将myaddons的路径添加到odoo.conf配置文件的addons_path参数中去
python odoo-bin scaffold huah_todo extra_addons
建立huah_todo addon在extra_addons,需将extra_addons的绝对路径添加到debian/odoo.conf中的addons_path参数中前端
# -*- 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', ], }
# -*- 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
python odoo-bin scaffold命令建立的views.xml其实都注释了编写xml的示例python
<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>
<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"这张表里。git
<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>
运行示例:
tree视图控制列表视图要显示的字段:
form视图控制建立数据时的显示:
点击已完成按钮会执行model的work_done方法修改结束时间和累计用时字段
github