


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、構(gòu) 建 Odoo 模 塊模塊組成o 業(yè)務(wù)對象業(yè)務(wù)對象聲明為類,由Odoo自動載入.o數(shù)據(jù)文件XML或CSV文件格式,在其中聲明了元數(shù)據(jù)(視圖或工作流)、配置數(shù)據(jù)(模塊參 數(shù))、演示數(shù)據(jù)等o Web控制器處理Web瀏覽器發(fā)來的requests.o靜態(tài)web數(shù)據(jù)Web用到的圖像,CSS或文件.模塊結(jié)構(gòu)一個Odoo模塊也是一個 Python 模塊,存放在一個目錄中,包含一個_init_.py文件, 用于導(dǎo)入其他Python模塊.from . import mymoduleodoo.py提供了一個子命令scaffold可以方便地創(chuàng)建一個空的模塊.where to put it >$ odoo.p
2、y scaffold vmodule n ame> <命令執(zhí)行后,將會創(chuàng)建一個子目錄并且其中包括了Odoo模塊所需的一些基本文件練習(xí)#1執(zhí)行./odoo.py scaffold openacademy addons,在 addons 目錄下創(chuàng)建一個名為 open academy 的模塊,生成的目錄文件結(jié)構(gòu)如下.openacademyI_i njt_.pyI_enerp_.pyIcon trollers.pyIdemo.xmlImodels.pyIsecurity1templates.xml各文件內(nèi)容請查看文件或查看,然后對openerp_.py中的幾種標(biāo)識文本進(jìn)行修改,至少需要添加
3、'installable':True, 'application':True。對象關(guān)系映射ORM層是Odoo的一個關(guān)鍵組件,它可以避免大部分的SQL語句編寫從而提高擴展性 和安全性.業(yè)務(wù)對象用派生自Model的Python類(模型)來編寫,該類的_name屬性定義了模型在 Odoo系統(tǒng)中的名稱.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 ()通用屬性與模型類似,字段也可以通過參數(shù)傳遞對其進(jìn)行設(shè)定:name = field. Char (required=True)字段的常用屬性有:o string (unicode, default: field ' s name)字段標(biāo)簽名稱,會顯示在界面上(對用戶可見)。o required (bool, default: False)如果值為True,此字段值不能為空,設(shè)置默認(rèn)值或者在創(chuàng)建記錄時提供。o help (unicode,
5、default:'')界面上顯示提示語。o in dex (bool, default: False)如果值為True,創(chuàng)建表時將為此列添加索引。簡單字段字段可以分為兩類:簡單字段和關(guān)系字段前者為原子值,直接保存在模型對應(yīng)的表中后者連接到其他的記錄上(可以是相同的模型也可以是不同的模型).Boolean. Date, Char 這些都是簡單字段.保留字段Odoo在模型中自動創(chuàng)建并維護(hù)一些字段,這些字段就是保留字段,這些字段數(shù)據(jù)不需 要也不應(yīng)該手動去修改.o id (Id)一條記錄的唯一 id。o create_date (Datetime)記錄創(chuàng)建時間。o create_uid
6、 (Ma ny2one)誰創(chuàng)建的記錄。o write_date (Datetime)最后修改時間。o write_uid (Ma ny2one)誰最后修改的記錄。特殊字段默認(rèn)情況下,Odoo 要求模型中有一個name字段,用于顯示和搜索,通過設(shè)置 _rec_name也可以達(dá)到這樣的目的.練習(xí)#2在 openacademy 模塊中定義一個新的模型 Course , openacademy/models.py內(nèi)容女口下:# -*- 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()數(shù)據(jù)文件Odoo是一個高度數(shù)據(jù)驅(qū)動的系統(tǒng),雖然使用Python代碼來定制模塊行為,但很多模塊 數(shù)據(jù)是在其載入時setup的,并且有些模塊僅僅為Odoo添加數(shù)據(jù).通過數(shù)據(jù)文件來定義模塊數(shù)據(jù),例如可以使用XML文件中的record元素定義數(shù)據(jù) 每一個record元素創(chuàng)建或者更新數(shù)據(jù)庫中的一條記錄,形式如下:<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),通過它可以引用到記錄(并且不需要知道記錄所在的數(shù)據(jù)庫ID).o 兀素name屬性用于確定字段名稱(
9、例如description),該元素的body給出字段的值.數(shù)據(jù)文件必須在模塊載入清單文件列表中,也就是_openerp_.py的data '列表(全部 載入)或demo '列表(只有設(shè)定為載入演示數(shù)據(jù)才會載入)中.練習(xí)#3創(chuàng)建一個數(shù)據(jù)文件來向Course中添加數(shù)據(jù),編輯openacademy/demo.xml,并確認(rèn)_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 >動作和菜單般通過數(shù)據(jù)文件來定義在Odoo中,動作和菜單都是定義在數(shù)據(jù)庫中的數(shù)據(jù)記錄 動作可以由三種方式觸發(fā)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的連接使用定義,數(shù)據(jù)文件在載入時順序地執(zhí)行,所以動作 的ID必須首先存在于數(shù)據(jù)庫中才能使用.練習(xí)#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中添加這個數(shù)據(jù)文件名到data '.'data':'templates.xm
19、l','views/ope nacademy.xml'.,更新模塊后可以看到菜單,操作看看效果.基本視圖視圖定義了模型數(shù)據(jù)如何顯示,每種類型的視圖代表一種數(shù)據(jù)可視化模式基本的視圖定義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中組合各種高層結(jié)構(gòu)Form用于創(chuàng)建或編輯單條記錄,根元素是<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 >練習(xí)#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",更新模塊,創(chuàng)建一個Course,可以看到form view 變了 .練習(xí)#6使用notebook .在form view中,將description字段放在一個tab中,方便隨后添加其他tabs,對練習(xí)#5的form view 數(shù)據(jù)做如下修改.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及其它統(tǒng)計/多條記錄視圖中的搜索字段.根元素為 <search>,其子元素定義了在哪
32、些字段上進(jìn)行搜索.vsearch >vfield name= "name" />vfield n ame= "i nven tor_id"/></ search >如果一個模型沒有定義對應(yīng)的Search view, odoo自動創(chuàng)建一個僅搜索name字段的search view.練習(xí)#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字段.模型中的關(guān)聯(lián)概述一個模型中的記錄可能關(guān)聯(lián)到其他模型的記錄,例如銷售訂單記錄會關(guān)聯(lián)到一個包含客 戶信息的客戶記錄練習(xí)#8為了說明數(shù)據(jù)關(guān)聯(lián),首先增加新的模型.Open Aca
35、demy 模塊中,一個session是一個在特定時間針對特定聽眾講授課程的過程 需要為session創(chuàng)建相應(yīng)的模型.session具有name,開始日期,持續(xù)時間以及座位數(shù)量等.此外還需要添加相應(yīng)的acti on 和 menu item 顯示模型數(shù)據(jù).首先在 openacademy/models.py 中創(chuàng)建 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)確定浮點數(shù)的精度,6表示總的數(shù)字位數(shù)(不包括小數(shù)點),2表示小數(shù)點后的位數(shù).所以,digits=(6,2)小數(shù)點前最多4位.關(guān)聯(lián)字段關(guān)聯(lián)
41、字段指向某些記錄,或者是相同的model(模型),或者是不同的model(模型)。關(guān)聯(lián)字段類型:練習(xí)#9概述? 使用many2one修改Course和Session模型(model),反映出與其他模型(model)的關(guān)聯(lián):? 每個Course有一個負(fù)責(zé)人,other_model值為 res.users ?每個 Session 有一個老師,other_model 值為 res.partner ? 一個 Session 關(guān)聯(lián)一個 Course,other_model 值為 openacademy.course ,必填? 調(diào)整view 。1.添加相關(guān)字段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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 學(xué)校物品放置管理辦法
- 托管資金項目管理辦法
- 實體廠家精密管理辦法
- 看護(hù)隊員使用管理辦法
- 如何取消訂單管理辦法
- 工地裝修工程管理辦法
- 工業(yè)食品工廠管理辦法
- 如何編寫資金管理辦法
- 廣西傷殘撫恤管理辦法
- 2025年上教科室文化建設(shè)計劃
- 旅行社導(dǎo)游帶團(tuán)操作流程
- 部編版小學(xué)道德與法治三年級下冊期末質(zhì)量檢測試卷【含答案】5套
- 怎樣當(dāng)好一名師長
- DB21T 3354-2020 遼寧省綠色建筑設(shè)計標(biāo)準(zhǔn)
- 新生兒復(fù)蘇解析課件
- (完整版)重大危險源清單及辨識表
- ABI7500熒光定量PCR儀標(biāo)準(zhǔn)操作規(guī)程
- 語言領(lǐng)域核心經(jīng)驗《學(xué)前兒童語言學(xué)習(xí)與發(fā)展核心經(jīng)驗》
- DB51T 5036-2017 四川省屋面工程施工工藝規(guī)程
- 11級設(shè)計題目寶豐紅四煤礦
- 08S305-小型潛水泵選用及安裝圖集
評論
0/150
提交評論