版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、构建Odoo模块模块组成o业务对象业务对象声明为Python类,由Odoo自动载入.o数据文件XMUg CSVC件格式,在其中声明了元数据(视图或工作流)、配置数据(模块参数)、 演示数据等.o Web空制器处理WebM览器发来的requests.o静态web数据Web用到的图像,CSS或JavaScript文件.模块结构一个Odoo模块也是一个 Python模块,存放在一个目录中,包含一个_init_.py 文件, 用于导入其他Python模块.from . import mymoduleodoo.py提供了一个子命令scaffold 可以方便地创建一个空的模块.$ odoo.py scaf
2、fold <module name> < where to put it >命令执行后,将会创建一个子目录并且其中包括了Odoo真块所需的一些基本文件.练习#1 执行 ./odoo.py scaffold openacademy addons, 在addons目录下仓建个 名为 openacademy的模块,生成的目录文件结构如下.openacademyI_init_.pyI_openerp_.pyIcontrollers.pyIdemo.xmlImodels.pyIsecurity1templates.xml各文件内容请查看文件或查看 原文,然后对openerp_.p
3、y中的几种标识文本进行修改,至少需要添加'installable':True, 'application':True。对象关系映射ORMg是Odoo的一个关键组件,它可以避免大部分的SQM句编写从而提高扩展性和安全 性.业务对象用派生自Model的Python类(模型)来编写,该类的_nameH性定义了模型在Odoo 系统中的名称.from openerp import modelsclass MinimalModel (models.Model)_name = 'test.model'字段字段定义模型能够存储什么以及在哪里存储,字段在模型类中用
4、属性来定义from openerp import models, fieldsclass LessMinimalModel (models.Model):_name = 'test.model2'name = fields. Char()通用属性与模型类似,字段也可以通过参数传递对其进行设定:name = field. Char(required=True)字段的常用属性有:o string (unicode, default: field' s name)字段标签名称,会显示在界面上(对用户可见)。o required (bool, default: False)如果
5、值为True,此字段值不能为空,设置默认值或者在创建记录时提供。o help (unicode, default:'')界面上显示提示语。o index (bool, default: False)如果值为True,创建表时将为此列添加索引。简单字段字段可以分为两类:简单字段和关系字段.前者为原子值,直接保存在模型对应的数据库 表中;后者连接到其他的记录上(可以是相同的模型也可以是不同的模型).Boolean, Date, Char这些都是简单字段.保留字段Odoo在模型中自动创建并维护一些字段,这些字段就是保留字段,这些字段数据不需要也 不应该手动去修改.o id (Id)一
6、条记录的唯一 id oo create_date (Datetime)记录创建时间。o create_uid (Many2one)谁创建的记录。o write_date (Datetime)最后修改时间。o write_uid (Many2one)谁最后修改的记录。特殊字段默认情况下,Odoo要求模型中有一个nam寸段,用于显示和搜索,通过设置_rec_name也 可以达到这样的目的.练习#2在 openacademy®块中定义个新的模型 Course, openacademy/models.py 内容如下:# -*- coding: utf-8 -*-from openerp im
7、port models, fields, apiclass Course(models.Model):_name = 'openacademy.course'name = fields.Char(string= "Title" , required= True)description = fields.Text()数据文件Odoo是一个高度数据驱动的系统,虽然使用Python代码来定制模块行为,但很多模块数 据是在其载入时setup的,并且有些模块仅仅为Odoo添加数据.通过数据文件来定义模块数据,例如可以使用XML±件中的<record&g
8、t;元素定义数据,每一 j<record>元素创建或者更新数据库中的一条记录,形式如下:<openerp><record model= "model name" id= "record identifier" ><field name-'a field name">a value </field ></ record ></data ><openerp>o modelOdoo模型名.o id外部ID(External Identifier),
9、 通过它可以引用到记录(并且不需要知道记录所在 的数据库ID).o 元素name属性用于确定字段名称(例如description),该元素的body给出字段的值.数据文件必须在模块载入清单文件列表中,也就是一openerp.py的data '列表(全部载 入)或demo列表(只有设定为载入演示数据才会载入)中.练习#3创建一个数据文件来向Course中添加数据,编辑openacademy/demo.xml,并确认_openerp_.py 的demo 歹!J表中有该文件 .<openerp><data ><record model= "openac
10、ademy.course" id= "course0" ><field name-'name">Course 0 </field ><fieldname-'description">Course 0's descriptionCan have multiple lines</ field ></ record ><record model= "openacademy.course" id= "coursel"
11、><field name-'name">Course 1 </field ><!- no description for this one -></ record ><record model= "openacademy.course" id= "course2" ><field name-'name">Course 2 </field ><field name-'description" >Cour
12、se 2's description</field ></ record ></data ></openerp>动作和菜单 在Odoo中,动作和菜单都是定义在数据库中的数据记录,一般通过数据文件来定义.动作可以由三种方式触发:o点击菜单项(菜单项链接到特定动作)o点击视图上的按钮(如果按钮连接到动作)o作为对象的上下文动作<record model= id= "action_list_ideas" ><fieldname=name”>Ideas</field><fieldnam
13、e=res_model” >idea.idea</field><fieldname=view_mode” >tree,form</field></record><menuitem id= "menu_ideas" parent= "menu_root" name=Ideas" sequence="10"action= "action_list_ideas" />注意:action必须先于menu的连接使用定义,数据文件在载入时顺序地执行,所
14、以动作 的ID必须首先存在于数据库中才能使用.练习#4定义一个新的菜单项访问OpenAcadem课程.仓I建openacademy/views/openacademy.xml文件,并在其中添力口动作和菜单 .<?xml version="1.0" encoding="UTF-8"?><openerp><data ><!- window action -><!-The following tag is an action definition for a "window action"
15、;, that is an action opening a view or a set of views<record model= id= "course_list_action" ><field name="name">Courses</field ><field name-'res_model" >openacademy.course </ field ><field name-'view_type" >form </ field &
16、gt;<field name-'view_mode" >tree,form </field ><field name-'help" type= "html" ><p class= "oe_view_nocontent_create" >Create the first course</p></ record ><!- top level menu: no parent -><menuitem id= "main_open
17、academy_menu'name-'Open Academy'/><!- A first level in the left side menu is neededbefore using action= attribute -><menuitem id= "openacademy_menu"name-'Open Academyparent="main_openacademy_menu/><!- the following menuitem should appear *after*its pa
18、rent openacademy_menu and *after* its action course_list_action -><menuitem id= "courses_menu" name="Courses" parent= "openacademy_menuaction="course_list_action" /><!- Full id location:action="openacademy.course_list_action"It is not required
19、 when it is the same module -></openerp>在openerp.py中添加这个数据文件名到data '.'data':'templates.xml','views/openacademy.xml',更新模块后可以看到菜单,操作看看效果.基本视图视图定义了模型数据如何显示,每种类型的视图代表一种数据可视化模式基本的视图定义<record model= id= "view_id" ><field name-'name">view.
20、name</ field ><field name-'model" >object_name </ field ><field name-'priority"eval= "16" /><field name-'arch" type= "xml" ><!- view content: <form>, <tree>, <graph>, .-></ record >Tree viewsTr
21、ee view 也被称为list views,在一个表格中显示记录.根元素是<tree>,最简形式的tree view只是简单地列出每条记录的多个字段,每个字段为一列.<tree string= "Idea list" ><field name="name"/><field name-'inventor_id" /></ tree >Form viewsForm用于创建或编辑单条记录,根元素是<form>,可以在form中组合各种高层结构元素 (如 groups,
22、notebooks) 以及交互元素(如 buttons, fields).<form string= "Idea form" ><group colspan= "4" ><group colspan= "2" col= "2" ><separator string= "General stuff" colspan= "2" /><field name-'name"/></group>&l
23、t;group colspan= "2" col= "2" ><separator string= "Dates" colspan= "2" /><field name-'active" /><field name-'invent_date"readonly= "1" /></group><notebook colspan= "4" ><page string- &qu
24、ot;Description" ><field name-'description"nolabel- "1" /></page></notebook ><field name-'state" /></group></ form >练习#5为 openacademy 仓建 form view, views/openacademy.xml 数据 文件中 增力口 <record model-" ” >内容.<?xml versio
25、n="1.0" encoding="UTF-8"?><openerp><data ><record model= id= "course_form_view" ><field name-'name">course.form </field ><field name-'model" >openacademy.course </field ><field name-'arch" type=
26、"xml" ><form string= "Course Form" ><sheet ><group><field name="name"/><field name='description"/></group ></ sheet ></form ></ record ><!- window action -><!-The following tag is an action defin
27、ition for a "window action",更新模块,创建一个Course,可以看到form view 变了.练习#6使用notebook.在form view 中,# description字段放在一个tab中,方便随后添加其他tabs,对练习#5的form view 数据做如下修改.<sheet ><group><field name-'name"/></group ><notebook><page string= "Description" >&l
28、t;field name-'description"/></page><page string= "About" >This is an example of notebooks</page></ notebook ></ sheet ></form ></ field >更新模块,看效果.More还可以使用HTM为form view提供更加灵活的布局,例如下面的例子.<form string= "Idea Form" ><hea
29、der><button string= "Confirm" type= "object" name=action_confirm”states= "draft" class= "oe_highlight" /><button string= "Mark as done" type= "object" name=action_done states= "confirmed" class= "oe_highlight"
30、; /><buttonstring= "Reset to draft"type= "object" name=action_draftstates= "confirmed,done" /><field name=state" widget= "statusbar" /></header><sheet><div class= "oe_title" ><labelfor= "name" class=
31、 "oe_edit_only" string= "Idea Name" /><h1><fieldname=name" /></h1></div><separator string= "General" colspan= "2" /><group colspan= "2" col= "2" ><fieldname=description" placeholder= "
32、;Idea description." /></group></sheet></form>Search viewsSearch views用来自定义list views及其它统计/多条记录视图中的搜索字段.根元素为<search>,其子元素定义了在哪些字段上进行搜索.<search ><field name-'name"/><field name-'inventor_id" /></ search >如果一个模型没有定义对应的 Search vie
33、w, odoo自动创建一个仅搜索 nametr段的search view.练习#7添力口 title 以及 description 搜索,在 views/openacademy.xml 中定义 search view.</ field ></ record ><record model= id= "course_search_view" ><field name-'name">course.search </ field ><field name-'model" >o
34、penacademy.course </field ><field name-'arch" type= "xml" ><search ><field name-'name"/></ search ></ field ></ record ><!- window action -><!-The following tag is an action definition for a "window action",更新模块
35、,搜索框输入字符后可以看到下方能够选择搜索description 字段.模型中的关联概述一个模型中的记录可能关联到其他模型的记录,例如销售订单记录会关联到一个包含客户 信息的客户记录.练习#8为了说明数据关联,首先增加新的模型.Open Academy真块中,一个session是一个在特定时间针对特定听众讲授课程的过程.需要为session创建相应的模型.session具有name,开始日期,持续时间以及座位数量等.此外还需要添加相应的action 和menuitem显示模型数据.首先在 openacademy/models.py 中仓建 Session 类.class Session (mo
36、dels.Model):_name = 'openacademy.sessionname = fields. Char(required=True)start_date = fields. Date()duration = fields. Float (digits=(6, 2), help= "Duration in days" )seats = fields. Integer (string= "Number of seats" )然后在 openacademy/view/openacademy.xml 中添力用于访问 session 模型的
37、 action 和 menuitem 定义.<!- Full id location:action="openacademy.course_list_action"It is not required when it is the same module -><!- session form view -><record model= id= "session_form_view" ><field name-'name">session.form </ field ><f
38、ield name-'model" >openacademy.session </ field ><field name-'arch" type= "xml" ><form string= "Session Form" ><sheet ><group><fieldname='name'7><fieldname="startdate" /><fieldname="duration”
39、 /><fieldname="seats” /></group ></ sheet ></form ></ field ></ record ><record model= id= "sessionlistaction"<fieldname-'name">Sessions </field ><fieldname-'res_model" >openacademy.session </ field<fi
40、eldname-'view_type" >form </ field ><fieldname-'view_mode" >tree,form </field ></ record ><menuitem id= "session_menu" name="Sessions”parent="openacademy_menu"action="session_list_action"/></data ></opener
41、p>digits=(6,2)确定浮点数的精度,6表示总的数字位数(不包括小数点),2表示小数点后的位数.所以,digits=(6,2)小数点前最多4位.关联字段关联字段指向某些记录,或者是相同的model(模型),或者是不同的model(模型)。关联字段类型:Many2one(other model, ondelete='set null')One2many(other model, related field)Many2many(other model)练习#9概述? 使用many2one#改Course和Session模型(model),反映出与其他模型(model)
42、的 关联:?每个 Course 有一个负责人,other_model 值为 res.users ? 每个 Session 有一个老师,other_model 值为 res.partner ? 一个 Session 关联一个 Course, other_model 值为 openacademy.course ,必填 ? 调整view o1 .添加相关字段Many2OneiiJ model2 .添加到view openacademy/models.pySession (models . Model)ipenacadem2), help="Nuinstructor_id = fields
43、. Many2one(res.partner', string ="Instructor" )course_id = fields . Many2one(openacademy.courseondelete ='cascade' , string ="Course" , required =True)openacademy/views/openacademy.xml<field name=name”/><field name=responsible_id” /></tree></field
44、></record><!- window action -><!-The following tag is an action definition for a "window action",<form string= "Session Form" ><sheet><group><group string= "General" ><field name=course_id” /><field name=name"/>
45、<field name=instructorid” />一</group><group string= "Schedule" ><field name=start_date” /><field name=duration” /><field name=seats” /><!- session tree/list view -><record model= id= "session_tree_view"<field name=name”>session.t
46、ree </field><field name=model” >openacademy.session </field><field name=arch" type="xml" ><tree string= "Session Tree" ><field name=name”/><field name=course_id” /></tree></field></record><record model= id= "
47、;session_list_action" ><field name=name">Sessions </field><field name=res_model" >openacademy.session </field>ExerciseInverse one2many relationsUsing the inverse relational field one2many, modify the models to reflect the relation between courses and session
48、s.1. Modify the? Course?class, and2. add the field in the course form view.openacademy/models.pyresponsible_id = fields . Many2one(res.users',ondelete='set null' , string -Responsible" , index =True)session_ids = fields . One2many('openacademy.session' , 'course_id'
49、, string ="Sessions")class Session (models . Model):openacademy/views/openacademy.xml<page string= "Description" ><field name=description" /></page><page string= "Sessions" ><field name=session_ids” ><tree string= "Registered s
50、essions" ><fieldname="name"/><fieldname="instructor_id"/></tree></field></page></notebook>ExerciseMultiple many2many relationsUsing the relational field many2many, modify the?Session?model to relate everysession to a set of?attendees. A
51、ttendees will be represented by partnerrecords, so we will relate to the built-in model?res.partner . Adapt the viewsaccordingly.1.2.Modify the? Session ?class, and add the field in the form view.openacademy/models.pyattendee_ids = fields . Many2many,es.partner', string ="Attendees")op
52、enacademy/views/openacademy.xmlInheritanceModel inheritanceOdoo provides two?inheritance?mechanisms to extend an existing model in a modular way.The first inheritance mechanism allows a module to modify the behavior of a model defined in another module:add fields to a model,override the definition o
53、f fields on a model,add constraints to a model,add methods to a model,override existing methods on a model.The second inheritance mechanism (delegation) allows to link every record of a model to a record in a parent model, and provides transparent access to the fields of the parent record.See also_i
54、nherit_inheritsView inheritanceInstead of modifying existing views in place (by overwriting them), Odoo provides view inheritance where children "extension" views are applied on top of root views, and can add or remove content from their parent.An extension view references its parent using
55、 the?inherit_id ?field, andinstead of a single view its? arch ?field is composed of any number of? xpath ?elements selecting and altering the content of their parent view:exprAn?XPath?expression selecting a single element in the parent view.Raises an error if it matches no element or more than onepo
56、sitionOperation to apply to the matched element:insideappends?xpath's body at the end of the matched elementreplacereplaces the matched element by the? xpath's bodybeforeinserts the? xpath's body as a sibling before the matched elementafterinserts the? xpaths's body as a sibling afte
57、r the matched elementattributesalters the attributes of the matched element using special? attribute ?elements in the? xpath's bodyTipWhen matching a single element, the? position ?attribute can be set directly on the element to be found. Both inheritances below will give the same result.Exercis
58、eAlter existing contentUsing model inheritance, modify the existing?Partner?model to addan?instructor ?boolean field, and a many2many field that correspondsto the session-partner relationUsing view inheritance, display this fields in the partner form viewNoteThis is the opportunity to introduce the
59、developer mode to inspect the view, find its external ID and the place to put the new field.1. Create a file?openacademy/partner.py ?and import it in?_init_.py2. Create a file?openacademy/views/partner.xml ?and add itto? _openerp_.pyopenacademy/_init_.py# -*- coding: utf-8 -*-from . import controlle
60、rsfrom . import modelsfrom . import partneropenacademy/_openerp_.py'templates.xml' ,'views/openacademy.xml',views/partner.xml',# only loaded in demonstration mode'demo':openacademy/partner.py# -*- coding: utf-8 -*-from openerp import fields, modelsclass Partner (models .
61、Model):_inherit = 'res.partner'# Add a new column to the res.partner model, by default partner s are not# instructorsinstructor = fields . Boolean( "Instructor" , default =False)session_ids = fields . Many2many(openacademy.session'string二"Attended Sessions" , readonly
62、 =True) openacademy/views/partner.xml<?xml version="1.0" encoding="UTF-8"?><openerp><data><!- Add instructor field to existing view -><record model= id= "partner_instructor_form_view" ><field name=name”>partner.instructor </field&g
63、t;<field name=model” >res.partner </field><field name=inherit_id" ref= "base.view_partner_form" /><field name=arch" type= "xml" ><notebook position= "inside" ><page string= "Sessions" ><group><field name=i
64、nstructor” /><field name=session_ids” /></group></page></notebook></field></record><record model= id= "contact_list_action" ><field name=name”>Contacts </field><field name=res_model” >res.partner </field><field name=vi
65、ew_mode” >tree,form </field></record><menuitem id= "configuration_menu" name=Configuration”parent= "main_openacademy_menu/><menuitem id= "contact_menu" name=Contacts”parent= "configuration_menu"action= "contact_list_action" />&l
66、t;/data></openerp>DomainsIn Odoo,? Domains?are values that encode conditions on records. A domain is a list of criteria used to select a subset of a model's records. Each criteria is a triple with a field name, an operator and a value.For instance, when used on the?Product?model the fol
67、lowing domain selects all?services?with a unit price over?1000:By default criteria are combined with an implicit AND. The logicaloperators? &?(AND),?| ?(OR) and?! ?(NOT) can be used to explicitly combine criteria. They are used in prefix position (the operator is inserted before its arguments rather
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 江苏扬州市广陵区育才教育集团2025-2026学年四年级上学期期末语文试题B卷(文字版含答案)
- 2026年银行对公运营外勤走访季度台账专员银行招聘考试笔试试题(含答案)
- 2026年烟草设备运维检修岗烟草公司招聘考试笔试试题(含答案)
- 幼儿园保育员个人年终总结5篇
- 2026年秋季小学开学第一课 防溺水安全教育
- 2026年秋季高中数学开学第一课 考试策略与技巧课件
- 2026年秋季幼儿园开学第一课 传统文化与民族精神
- 药店营业员转正试题及答案
- 职业暴露防护试题及答案2026年全新版
- 2026年《医务人员职业暴露与防护》培训考试试题(附答案)
- 2026江苏徐州市市级机关印刷厂有限公司招聘工作人员2人笔试题库附答案详解(基础题)
- 2026年特种设备P4液化石油气瓶充装模拟考试题库试卷及答案
- 2026年宁夏中考(数学)真题含答案
- 消防文员会计试题及答案2026年
- 医院手术室净化装修工程技术交底报告
- 2026年贵州遵钛集团有限责任公司校园招聘考试模拟试题及答案解析
- 企业邮箱使用规范及邮件格式标准
- 2025年度妇产科实习同学出科考试题及答案
- 2026年四川大学基础学科拔尖计划面试试题含答案
- 超市食品加工管理制度
- 2023年普通高等学校招生全国统一考试新课标全国Ⅰ卷数学真题(解析版)
评论
0/150
提交评论