Odoo模块开发全流程_第1页
Odoo模块开发全流程_第2页
Odoo模块开发全流程_第3页
已阅读5页,还剩58页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、构 建 Odoo 模 块模块组成o 业务对象业务对象声明为类,由Odoo自动载入.o数据文件XML或CSV文件格式,在其中声明了元数据(视图或工作流)、配置数据(模块参 数)、演示数据等o Web控制器处理Web浏览器发来的requests.o静态web数据Web用到的图像,CSS或文件.模块结构一个Odoo模块也是一个 Python 模块,存放在一个目录中,包含一个_init_.py文件, 用于导入其他Python模块.from . import mymoduleodoo.py提供了一个子命令scaffold可以方便地创建一个空的模块.where to put it >$ odoo.p

2、y scaffold vmodule n ame> <命令执行后,将会创建一个子目录并且其中包括了Odoo模块所需的一些基本文件练习#1执行./odoo.py scaffold openacademy addons,在 addons 目录下创建一个名为 open academy 的模块,生成的目录文件结构如下.openacademyI_i njt_.pyI_enerp_.pyIcon trollers.pyIdemo.xmlImodels.pyIsecurity1templates.xml各文件内容请查看文件或查看,然后对openerp_.py中的几种标识文本进行修改,至少需要添加

3、'installable':True, 'application':True。对象关系映射ORM层是Odoo的一个关键组件,它可以避免大部分的SQL语句编写从而提高扩展性 和安全性.业务对象用派生自Model的Python类(模型)来编写,该类的_name属性定义了模型在 Odoo系统中的名称.from openerp import models_n ame = 'test.mode l'字段字段定义模型能够存储什么以及在哪里存储,字段在模型类中用属性来定义from openerp import models, fieldsclassLessMi

4、 nimalModel(models.Model)_n ame = 'test.model2'name = fields.Char ()通用属性与模型类似,字段也可以通过参数传递对其进行设定:name = field. Char (required=True)字段的常用属性有:o string (unicode, default: field ' s name)字段标签名称,会显示在界面上(对用户可见)。o required (bool, default: False)如果值为True,此字段值不能为空,设置默认值或者在创建记录时提供。o help (unicode,

5、default:'')界面上显示提示语。o in dex (bool, default: False)如果值为True,创建表时将为此列添加索引。简单字段字段可以分为两类:简单字段和关系字段前者为原子值,直接保存在模型对应的表中后者连接到其他的记录上(可以是相同的模型也可以是不同的模型).Boolean. Date, Char 这些都是简单字段.保留字段Odoo在模型中自动创建并维护一些字段,这些字段就是保留字段,这些字段数据不需 要也不应该手动去修改.o id (Id)一条记录的唯一 id。o create_date (Datetime)记录创建时间。o create_uid

6、 (Ma ny2one)谁创建的记录。o write_date (Datetime)最后修改时间。o write_uid (Ma ny2one)谁最后修改的记录。特殊字段默认情况下,Odoo 要求模型中有一个name字段,用于显示和搜索,通过设置 _rec_name也可以达到这样的目的.练习#2在 openacademy 模块中定义一个新的模型 Course , openacademy/models.py内容女口下:# -*- cod ing: utf-8 -*-from openerp import models, fields, apiclass Course (models.Model)

7、 :_n ame ='ope nacademy.course'name = fields.Char(string="Title" , required= True )descripti on = fields.Text()数据文件Odoo是一个高度数据驱动的系统,虽然使用Python代码来定制模块行为,但很多模块 数据是在其载入时setup的,并且有些模块仅仅为Odoo添加数据.通过数据文件来定义模块数据,例如可以使用XML文件中的record元素定义数据 每一个record元素创建或者更新数据库中的一条记录,形式如下:<openerp ><

8、;data ><record model= "model n ame" id= "record ide ntifier"><fieldname= "a field name" >a value </ field ></ record ></ data ><openerp >o modelOdoo模型名.o id外部ID(External Identifier),通过它可以引用到记录(并且不需要知道记录所在的数据库ID).o 兀素name属性用于确定字段名称(

9、例如description),该元素的body给出字段的值.数据文件必须在模块载入清单文件列表中,也就是_openerp_.py的data '列表(全部 载入)或demo '列表(只有设定为载入演示数据才会载入)中.练习#3创建一个数据文件来向Course中添加数据,编辑openacademy/demo.xml,并确认_openerp_.py 的demo '列表中有该文件 .<openerp ><data ><record model= "ope nacademy.course" id= "course。&qu

10、ot; ><fieldname= "name" >Course 0 </ field ><fieldn ame= "descripti on" >Course 0's descripti onCan have multiple lines</ record >vrecord model= "ope nacademy.course"id="coursel"vfieldname= "name" >Course 1 </ fie

11、ld<!- no descripti on for this one -></ record >vrecord model= "ope nacademy.course"id="course2"vfieldname= "name" >Course 2 </ field<field n ame= "descripti on">Course 2's descripti on</ field ></ record ></ data >

12、;</ openerp >动作和菜单般通过数据文件来定义在Odoo中,动作和菜单都是定义在数据库中的数据记录 动作可以由三种方式触发o点击菜单项(菜单项链接到特定动作)o点击视图上的按钮(如果按钮连接到动作)o作为对象的上下文动作<record model= id= "actio nist_ideas"><fieldname="name" >ldeas</field><fieldn ame="res_model" >idea.idea</field>v/recor

13、d><me nuitemid= "me nu_ideas"pare nt= "me nu_root"n ame="ldeas"seque nce= "10"acti on= "acti on _list_ideas"/>注意:action必须先于menu的连接使用定义,数据文件在载入时顺序地执行,所以动作 的ID必须首先存在于数据库中才能使用.练习#4定义一个新的菜单项访问Ope nAcademy 课程.仓U建openacademy/views/openacademy.xml文

14、件,并在其中添加动作和菜单.<?xml version="1.0" encoding="UTF-8"?><openerp ><data ><!- wi ndow acti on -><!-The followi ng tag is an acti on defi niti on for a "wi ndow actio n",that is an acti on ope ning a view or a set of views-><record model= id=&

15、quot;courseist_actio n"><field name= "name" >Courses </ field ><field name= "res_model" >openacademy.course </ field ><fieldname= "view_type">form </ field ><fieldname= "view_mode" >tree,form </ field >&l

16、t;/ p></ field ></ record ><!- top level menu: no pare nt -><me nuitem id= "ma in _ope nacademy_me nu"n ame= "Ope n Academy" /><!- A first level in the left side menu is neededbefore using acti on= attribute -><me nuitem id= "ope nacademy_

17、me nu"n ame= "Ope n Academy"pare nt="main_ope nacademy_me nu"/><!- the following menuitem should appear *after*its pare nt ope nacademy_me nu and *after* itsacti on course_list_acti on -><me nuitem id= "courses_me nu"n ame= "Courses"pare nt=&q

18、uot;ope nacademy_me nu"acti on="course_list_acti on"/><!- Full id locati on:acti on="ope nacademy.course_list_acti on"It is not required when it is the same module -></ data ></ openerp >在_openerp_.py中添加这个数据文件名到data '.'data':'templates.xm

19、l','views/ope nacademy.xml'.,更新模块后可以看到菜单,操作看看效果.基本视图视图定义了模型数据如何显示,每种类型的视图代表一种数据可视化模式基本的视图定义vrecord model= id= "view_id" ><fieldname= "name" > </ field >vfieldname= "model" >object_name </ field><fieldname= "priority&q

20、uot; eval= "16" /><fieldname= "arch" type= "xml" ><!- view content: <form>, <tree>, <graph>, .-></ field ></ record >Tree viewsTree view也被称为list views,在一个表格中显示记录.根元素是<tree>,最简形式的tree view只是简单地列出每条记录的多个字段,每个字段为一列.<tre

21、e stri ng="Idea list"<fieldname= "name" /></ tree >Form views中组合各种高层结构Form用于创建或编辑单条记录,根元素是<form>,可以在form元素(女口 groups, notebooks) 以及交互元素(如 buttons, fields).<form string= "Idea form" >vgroup colspan= "4" ><group colspan="2"

22、; col= "2" >separator string="General stuff" colspan= "2" />vfieldname= "name" />vfieldn ame= "inven tor_id"/></ group ><group colspan="2" col= "2" ><separator string= "Dates" colspan="2&q

23、uot; /><fieldn ame= "active"/><fieldn ame= "inven t_date"readonly="1" /></ group ><no tebookcolspan= "4" ><pagestri ng= "Descripti on"><field name= "description" nolabel= "1" /></ page >

24、;</ notebook ><fieldn ame= "state" /></ group ></ form >练习#5v?xml version="1.0" encoding="UTF-8"?><openerp >vdata >vrecord model= id= "course_form_view" ><fieldname= "name" >course.form </ field >vfi

25、eldname= "model" >openacademy.course</ fieldvfieldname= "arch" type= "xml" >vform string="Course Form" >vsheet >vgroup >vfieldname= "name" />vfieldn ame= "descripti on"/></ group ></ sheet ></ form >

26、;</ field ></ record ><!- wi ndow acti on -><!-The following tag is an action definition for a "window action",更新模块,创建一个Course,可以看到form view 变了 .练习#6使用notebook .在form view中,将description字段放在一个tab中,方便随后添加其他tabs,对练习#5的form view 数据做如下修改.vsheet >vgroup ><fieldname=

27、 "name" /></ group ><notebook ><page string= "Description" ><fieldn ame= "descripti on"/></ page ><page string="About" >This is an example of no tebooks</ page ></ notebook ></ sheet ></ form ><

28、/ field >更新模块,看效果More还可以使用HTML为form view 提供更加灵活的布局,例如下面的例子.<form stri ng="Idea Form" ><header>vbutt onstri ng="Con firm"type= "object"n ame="acti on con firm"states="draft"class= "oe_highlight"/><butt onstri ng="Ma

29、rk as done"type= "object"name="action done"states="con firmed"class= "oe_highlight"/><butt onstri ng="Reset to draft"type= "object"name="action draft"<field</header><sheet><div<labelstates="con

30、 firmed,do ne"n ame="state"widget=/>"statusbar" />class="oe title"for= "n ame" class="oe_edit_ only"stri ng= "Idea Name"/><h1><fieldn ame="n ame"/></h1></div><separatorstri ng="Ge nera

31、l"colspa n= "2"/><groupcolspa n= "2" col= "2"<fieldn ame="descriptio n"placeholder="Idea descripti on."/></group></sheet></form>Search viewsSearch views用来自定义list views及其它统计/多条记录视图中的搜索字段.根元素为 <search>,其子元素定义了在哪

32、些字段上进行搜索.vsearch >vfield name= "name" />vfield n ame= "i nven tor_id"/></ search >如果一个模型没有定义对应的Search view, odoo自动创建一个仅搜索name字段的search view.练习#7添力卩 title 以及 description 搜索,在 views/openacademy.xml中定义 search view.</ field ></ record ><record model= id=

33、 "course_search_view" ><field name= "name" >course.search </ field ><field name= "model" >openacademy.course </ field ><field name= "arch" type= "xml" ><search ><field name= "name" /><field n

34、ame= "descripti on"/></ search ></ record ><!- wi ndow acti on -><!-The following tag is an action definition for a "window action",更新模块,搜索框输入字符后可以看到下方能够选择搜索description字段.模型中的关联概述一个模型中的记录可能关联到其他模型的记录,例如销售订单记录会关联到一个包含客 户信息的客户记录练习#8为了说明数据关联,首先增加新的模型.Open Aca

35、demy 模块中,一个session是一个在特定时间针对特定听众讲授课程的过程 需要为session创建相应的模型.session具有name,开始日期,持续时间以及座位数量等.此外还需要添加相应的acti on 和 menu item 显示模型数据.首先在 openacademy/models.py 中创建 Session 类.classSession (models.Model)_n ame ='ope nacademy.sessi on'n ame = fields.Char (required=True)start_date = fields.Date ()durati

36、on = fields.Float (digits=(6, 2), help="Duration in days" )seats = fields.In teger (stri ng= "Number of seats" )然后在 openacademy/view/openacademy.xml中添加用于访问 session 模型的 action和menu item 定义.<!- Full id locati on:acti on="ope nacademy.course_list_acti on"It is not requi

37、red when it is the same module -><!- sessi on form view -><record model= id="sessi on_ form_view"><fieldname= "name" >session.form</ field ><fieldname= "model" >openacademy.session</ field ><fieldname= "arch" type= &qu

38、ot;xml" ><form string="Session Form" ><sheet ><group ><fieldname="name" /><fieldn ame="start_date" /><fieldn ame="durati on" /><fieldn ame="seats" /></ group ></ sheet ></ form ><

39、/ record >vrecord model= id="sessi on _list_acti on">vfieldname= "name" >Sessions </ field >vfieldn ame="res_model"vfieldn ame="view_type"vfieldn ame="view_mode"</ record >>openacademy.session</ field >>form </ fie

40、ld >>tree,form </ field >n ame= "Sessi ons"<menuitem id= "session_menu"pare nt="ope nacademy_me nu"action="session list action"/></ data ></ openerp >digits=(6,2)确定浮点数的精度,6表示总的数字位数(不包括小数点),2表示小数点后的位数.所以,digits=(6,2)小数点前最多4位.关联字段关联

41、字段指向某些记录,或者是相同的model(模型),或者是不同的model(模型)。关联字段类型:练习#9概述? 使用many2one修改Course和Session模型(model),反映出与其他模型(model)的关联:? 每个Course有一个负责人,other_model值为 res.users ?每个 Session 有一个老师,other_model 值为 res.partner ? 一个 Session 关联一个 Course,other_model 值为 openacademy.course ,必填? 调整view 。1.添加相关字段Many20ne至U model2.添加到vi

42、ewope nacademv/models.description.Text()responsible_id=fields . Many2one( 'res.users'ondeleteclass Sessi,require="Number of se.),string ="Instructor" ),string ="Responsible" , index =True)='set null'namestartdurationseats=fields . Float(digits =(6, 2), help =

43、"Dur=fields . Integer(stringinstructor id=fields . Many2one('res.partner'course idondelete=fields . Many2one( 'openacademy.course'-cascade' , string ="Course" , required =True)ope nacademy/views/ope nacademy.xml<sheet><group><fieldname='name"

44、;/><fieldname='responsible_id"/></field></record></group><notebook><pagestring= "Description"><!- override the automatically generated list view for courses -><record model= id= "course_tree_view" ><field name='na

45、me" >course.tree </field><fieldname='model" >openacademy.course </field><fieldname='arch" type= "xml" ><tree string= "Course Tree" ></field></record><!- window action -><!-The following tag is an action

46、definition for a "window action",<form string= "Session Form" ><sheet><group><group string= "General" ><fieldname='course_id"/><field name='name" /><field name='instructor_id" /></group><group

47、 string= "Schedule" ><fieldname='start_date"/><fieldname='duration"/><fieldname='seats" /></group></group></sheet></form></field></record><!- session tree/list view -><record model= id= "sessi

48、on_tree_view" ><field name='name" >session.tree </field><fieldname='model" >openacademy.session </field><fieldname='arch" type= "xml" ><tree string= "Session Tree" ><fieldname='name" /><field

49、name='course_id" /></tree></field></record>ExerciseIn verse on e2ma ny relati onsUsing the in verse relati onal field on e2ma ny, modify the models to reflect the relati on betwee n courses and sessi ons.1. Modify the? Course ?class, and2. add the field in the course fo

50、rm view.:set nu,string ="Respope nacademy/models.py=True)session_ids = fields . One2many(,string ="Sessions")'openacademy.session' , 'course_id'ope nacademy/views/ope nacademy.xmlExerciseMultiple many 2ma ny relati onsUsing the relational field many2many, modify the?Se

51、ssion?model to relate every session to a set of?attendees. Attendees will be represented by partner records, so we will relate to the built-in model?res.part ner . Adapt the views accord in gly.1. Modify the? Session ?class, and2. add the field in the form view.ope nacademy/models.pycourse idondelet

52、e-fields . Many2one('openacademy.cou-'cascade' , string -"Course"structor'attendee idsingquired -True)=fields . Many2many(res.partner',string-'Attendees")ope nacademy/views/ope nacademy.xmlIn heritanceModel in herita neeOdoo provides two?i nherita nce?mecha nis

53、ms to exte nd an exist ing model in amodular way.The first inheritanee mechanism allows a module to modify the behavior of amodel defi ned in ano ther module:add fields to a model,override the definition of fields on a model,add con stra ints to a model,add methods to a model,override existing metho

54、ds on a model.The second inheritance mechanism (delegation) allows to link every record of a model to a record in a pare nt model, and provides tran spare nt access to the fields of the pare nt record.See alsoView inheritanceIn stead of modify ing exist ing views in place (by overwriti ng them), Odo

55、o provides view in herita nce where childre n "exte nsion" views are applied on top of root views, and can add or remove content from their pare nt.An exte nsion view referen ces its pare nt using the?in herit_id ?field, and in stead of asingle view its? arch ?field is composed of any numb

56、er of?xpath ?elements selectingand alteri ng the content of their pare nt view:exprAn? ?expressi on select ing a sin gle eleme nt in the pare nt view. Raises an error if it matches no element or more than onepositi onOperati on to apply to the matched eleme nt:in sideappends? xpath 's body at th

57、e end of the matched elementreplacereplaces the matched eleme nt by the? xpath 's bodybeforeinserts the? xpath 's body as a sibling before the matched elementafterinserts the? xpaths 's body as a sibling after the matched elementattributesalters the attributes of the matched element usin

58、gspecial? attribute ?elements in the? xpath's bodyTippositi on ?attribute can be setWhe n match ing a sin gle eleme nt, the?directly on the element to be found. Both inheritances below will give the same result.ExerciseAlter existing contentUsing model inheritanee, modify the existing?Partner?mo

59、del to addan? in structor?boolea n field, and a many 2ma ny field that corresp onds tothe session-partner relationUsing view inheritanee, display this fields in the partner form viewNoteThis is the opport un ity to in troduce the developer mode to in spect theview, find its external ID and the place

60、 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 it to? _openerp_.pyopenacademy/_init_.pyopenacademy/_openerp_.py'templates.xml' ,'views/openacademy.xml' ,'views/partner.xml' ,# on

61、ly loaded in demonstration mode'demo':ope nacademy/part ner.py# -*- coding: utf-8 -*-from openerp import fields, modelsclass Partner (models . Model):_inherit = 'res.partner'# Add a new column to the res.partner model, by default partners are not# instructorsinstructor = fields . Boo

62、lean( "Instructor" , default =False)session_ids = fields . Many2many(openacademy.session' ,string ="Attended Sessions" , readonly =True)ope nacademy/views/part ner.xml<?xml version="1.0" encoding="UTF-8"?><openerp><data><!- Add instruc

63、tor field to existing view -><record model= id= "partner_instructor_form_view" ><fieldname='name" >partner.instructor</field><fieldname='model" >res.partner </field><fieldname='inherit_id"ref= "base.view_partner_form"

64、 /><fieldname='arch" type= "xml" ><notebook position= "inside" ><page string= "Sessions" ><group><fieldname='instructor"/></notebook>|</field></record><record model= id= "contact_list_action&qu

65、ot; ><fieldname='name" >Contacts </field><fieldname='res_model" >res.partner </field><fieldname='view_mode" >tree,form </field> v/record>vmenuitem id= "configuration_menu" name=Configuration parent= "main_openacademy

66、_menu" />vmenuitem id= "contact menu" name=Contacts"parent= "configuration_menu"action= "contact_list_action" /></data></openerp>Domai nsIn Odoo,?are values that en code con diti ons on records. A doma in 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 instanee, when used on the?Product?model the following domain selects all?service

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论