odoo 的字段。orm对象
OpenERP ORM 對(duì)象方法列表
OpenERP對(duì)象支持的字段類型有,基礎(chǔ)類型:char, text, boolean, integer, float, date, time, datetime, binary;復(fù)雜類型:selection, function, related;關(guān)系類型:one2one, one2many, many2one, many2many。下面逐一說明。
boolean: 布爾型(true, false)
integer: 整數(shù)。
float: 浮點(diǎn)型,如 'rate' : fields.float('Relative Change rate',digits=(12,6)), digits定義整數(shù)部分和小數(shù)部分的位數(shù)。
char: 字符型,size屬性定義字符串長(zhǎng)度。
text: 文本型,沒有長(zhǎng)度限制。
date: 日期型
datetime: 日期時(shí)間型
binary: 二進(jìn)制型
function: 函數(shù)型,該類型的字段,字段值由函數(shù)計(jì)算而得,不存儲(chǔ)在數(shù)據(jù)表中。其定義格式為:
fields.function(fnct, arg=None, fnct_inv=None, fnct_inv_arg=None, type='float', fnct_search=None, obj=None, method=False, store=True)
· type 是函數(shù)返回值的類型。
· method 為True表示本字段的函數(shù)是對(duì)象的一個(gè)方法,為False表示是全局函數(shù),不是對(duì)象的方法。如果method=True,obj指定method的對(duì)象。
· fcnt 是函數(shù)或方法,用于計(jì)算字段值。如果method = true, 表示fcnt是對(duì)象的方法,其格式如下:def fnct(self, cr, uid, ids, field_name, args, context),否則,其格式如下:def fnct(cr, table, ids, field_name, args, context)。ids是系統(tǒng)傳進(jìn)來的當(dāng)前存取的record id。field_name是本字段名,當(dāng)一個(gè)函數(shù)用于多個(gè)函數(shù)字段類型時(shí),本參數(shù)可區(qū)分字段。args是'arg=None'傳進(jìn)來的參數(shù)。
· fcnt_inv 是用于寫本字段的函數(shù)或方法。如果method = true, 其格式是:def fcnt_inv(self, cr, uid, ids, field_name, field_value, args, context),否則格式為:def fcnt_inv(cr, table, ids, field_name, field_value, args, context)
· fcnt_search 定義該字段的搜索行為。如果method = true, 其格式為:def fcnt_search(self, cr, uid, obj, field_name, args),否則格式為:def fcnt_search(cr, uid, obj, field_name, args)
· store 表示是否希望在數(shù)據(jù)庫(kù)中存儲(chǔ)本字段值,缺省值為False。不過store還有一個(gè)增強(qiáng)形式,格式為 store={'object_name':(function_name,['field_name1','field_name2'],priority)} ,其含義是,如果對(duì)象'object_name'的字段['field_name1','field_name2']發(fā)生任何改變,系統(tǒng)將調(diào)用函數(shù)function_name,函數(shù)的返回結(jié)果將作為參數(shù)(arg)傳送給本字段的主函數(shù),即fnct。
selection: 下拉框字段。定義一個(gè)下拉框,允許用戶選擇值。如:'state': fields.selection((('n','Unconfirmed'),('c','Confirmed')),'State', required=True),這表示state字段有兩個(gè)選項(xiàng)('n','Unconfirmed')和('c','Confirmed')。
one2one: 一對(duì)一關(guān)系,格式為:fields.one2one(關(guān)聯(lián)對(duì)象Name, 字段顯示名, ... )。在V5.0以后的版本中不建議使用,而是用many2one替代。
many2one: 多對(duì)一關(guān)系,格式為:fields.many2one(關(guān)聯(lián)對(duì)象Name, 字段顯示名, ... )。可選參數(shù)有:ondelete,可選值為"cascade"和"null",缺省值為"null",表示one端的record被刪除后,many端的record是否級(jí)聯(lián)刪除。
one2many: 一對(duì)多關(guān)系,格式為:fields.one2many(關(guān)聯(lián)對(duì)象Name, 關(guān)聯(lián)字段, 字段顯示名, ... ),例:'address': fields.one2many('res.partner.address', 'partner_id', 'Contacts')。
many2many: 多對(duì)多關(guān)系。例如:
'category_id':fields.many2many('res.partner.category','res_partner_category_rel','partner_id','category_id','Categories'),
表示以多對(duì)多關(guān)系關(guān)聯(lián)到對(duì)象res.partner.category,關(guān)聯(lián)表為'res_partner_category_rel',關(guān)聯(lián)字段為'partner_id'和'category_id'。當(dāng)定義上述字段時(shí),OpenERP會(huì)自動(dòng)創(chuàng)建關(guān)聯(lián)表為'res_partner_category_rel',它含有關(guān)聯(lián)字段'partner_id'和'category_id'。
reference: 引用型,格式為:fields.reference(字段名, selection, size, ... )。其中selection是: 1)返回tuple列表的函數(shù),或者 2)表征該字段引用哪個(gè)對(duì)象(or model)的tuples列表。reference字段在數(shù)據(jù)庫(kù)表中的存儲(chǔ)形式是(對(duì)象名,ID),如(product.product,3)表示引用對(duì)象product.product(數(shù)據(jù)表product_product)中id=3的數(shù)據(jù)。reference的例子:
def _links_get(self, cr, uid):
cr.execute('select object,name from res_request_link order by priority')
return cr.fetchall()
...
'ref':fields.reference('Document Ref 2', selection=_links_get, size=128),
...
上例表示,字段ref可以引用哪些對(duì)象類型的resource,可引用的對(duì)象類型從下拉框選擇。下拉框的選項(xiàng)由函數(shù)_links_get返回,是(object,name)對(duì)的列表,如[("product.product","Product"), ("account.invoice","Invoice"), ("stock.production.lot","Production Lot")] 。
related: 關(guān)聯(lián)字段,表示本字段引用關(guān)聯(lián)表中的某字段。格式為:fields.related(關(guān)系字段,引用字段,type, relation, string, ...),關(guān)系字段是本對(duì)象的某字段(通常是one2many or many2many),引用字段是通過關(guān)系字段關(guān)聯(lián)的數(shù)據(jù)表的字段,type是引用字段的類型,如果type是many2one or many2many, relation指明關(guān)聯(lián)表。例子如下:
'address': fields.one2many('res.partner.address', 'partner_id', 'Contacts'),
'city':fields.related('address','city',type='char', string='City'),
'country':fields.related('address','country_id',type='many2one', relation='res.country', string='Country'),
這里,city引用address的city字段,country引用address的country對(duì)象。在address的關(guān)聯(lián)對(duì)象res.partner.address中,country_id是many2one類型的字段,所以type='many2one', relation='res.country'。
property: 屬性字段,下面以具體例子解說property字段類型。
'property_product_pricelist': fields.property('product.pricelist', type='many2one', relation='product.pricelist',string="Sale Pricelist", method=True, view_load=True, group_name="Pricelists Properties")
這個(gè)例子表示,本對(duì)象通過字段'property_product_pricelist'多對(duì)一(type='many2one')關(guān)聯(lián)到對(duì)象product.pricelist(relation='product.pricelist')。和many2one字段類型不同的是,many2one字段會(huì)在本對(duì)象中創(chuàng)建數(shù)據(jù)表字段'property_product_pricelist',property字段類型不會(huì)創(chuàng)建數(shù)據(jù)表字段'property_product_pricelist'。property字段類型會(huì)從數(shù)據(jù)表ir.property中查找name='property_product_pricelist'(即字段定義中的'product.pricelist'加上前綴property,并將"."替換成"_"作為name)且company_id和本對(duì)象相同的記錄,從該記錄的value字段(value字段類型為reference)查得關(guān)聯(lián)記錄,如(product.pricelist,1),表示本對(duì)象的resource多對(duì)一關(guān)聯(lián)到對(duì)象product.pricelist的id=1的記錄。也就是說,property字段類型通過ir.property間接多對(duì)一關(guān)聯(lián)到別的對(duì)象。
property字段類型基本上和many2one字段類型相同,但是有兩種情況優(yōu)于many2one字段。其一是,例如,當(dāng)有多條記錄通過ir.property的name='property_product_pricelist'的記錄關(guān)聯(lián)到記錄(product.pricelist,1),此時(shí),如果希望將所有關(guān)聯(lián)關(guān)系都改成關(guān)聯(lián)到記錄(product.pricelist,2)。如果是many2one類型,不寫代碼,很難完成此任務(wù),是property字段的話,只要將ir.property中的value值(product.pricelist,1)改成(product.pricelist,2),則所有關(guān)聯(lián)關(guān)系都變了。修改ir.property的value值可以在系統(tǒng)管理下的菜單Configuration --> Properties中修改。其二是,例如,同一業(yè)務(wù)伙伴,但希望A公司的用戶進(jìn)來看到的該業(yè)務(wù)伙伴價(jià)格表為pricelistA,B公司的用戶進(jìn)來看到的該業(yè)務(wù)伙伴價(jià)格表為pricelistB,則many2one類型達(dá)不到該效果。property類型通過ir.property中的記錄關(guān)聯(lián)時(shí)加上了company_id的條件,因此可以使得不同公司的員工進(jìn)來看到不同的關(guān)聯(lián)記錄。
由于property類型通過ir.property關(guān)聯(lián),因此,每個(gè)property類型的字段都必須在ir.property中有一條關(guān)聯(lián)記錄。這可以在安裝時(shí)導(dǎo)入該條記錄,參考代碼如下:
<record model="ir.property" id="property_product_pricelist">
<field name="name">property_product_pricelist</field>
<field name="fields_id" search="[('model','=','res.partner'), ('name','=','property_product_pricelist')]"/>
<field name="value" eval="'product.pricelist,'+str(list0)"/>
</record>
字段定義的參數(shù)
字段定義中可用的參數(shù)有, change_default,readonly,required,states,string,translate,size,priority,domain,invisible,context,selection。
change_default:別的字段的缺省值是否可依賴于本字段,缺省值為:False。例子(參見res.partner.address),
'zip': fields.char('Zip', change_default=True, size=24),
這個(gè)例子中,可以根據(jù)zip的值設(shè)定其它字段的缺省值,例如,可以通過程序代碼,如果zip為200000則city設(shè)為“上海”,如果zip為100000則city為“北京”。
readonly: 本字段是否只讀,缺省值:False。
required: 本字段是否必須的,缺省值:False。
states: 定義特定state才生效的屬性,格式為:{'name_of_the_state': list_of_attributes},其中l(wèi)ist_of_attributes是形如[('name_of_attribute', value), ...]的tuples列表。例子(參見account.transfer):
'partner_id': fields.many2one('res.partner', 'Partner', states={'posted':[('readonly',True)]}),
string: 字段顯示名,任意字符串。
translate: 本字段值(不是字段的顯示名)是否可翻譯,缺省值:False。
size: 字段長(zhǎng)度。
priority:
domain: 域條件,缺省值:[]。在many2many和many2one類型中,字段值是關(guān)聯(lián)表的id,域條件用于過濾關(guān)聯(lián)表的record。例子:
'default_credit_account_id': fields.many2one('account.account', 'Default Credit Account', domain="[('type','!=','view')]"),
本例表示,本字段關(guān)聯(lián)到對(duì)象('account.account')中的,type不是'view'的record。
invisible: 本字段是否可見,即是否在界面上顯示本字段,缺省值True。
selection: 只用于reference字段類型,參見前文reference的說明。
> 另外一個(gè)問題就是function的store=true有沒有意義呢,不是每次讀取這個(gè)function就會(huì)執(zhí)行里面的方法,那么還儲(chǔ)存在數(shù)據(jù)庫(kù)里面有什么作用?
我的理解是store=true可以將每次函數(shù)計(jì)算的結(jié)果存儲(chǔ)到數(shù)據(jù)庫(kù)中,這樣有兩個(gè)好處,一是便于DBA從數(shù)據(jù)庫(kù)查看數(shù)據(jù);二是非OpenERP的軟件可以直接訪問數(shù)據(jù)庫(kù)共享數(shù)據(jù)。另外補(bǔ)充一點(diǎn),fcnt_inv不限于寫入本對(duì)象的數(shù)據(jù)表,更經(jīng)常的情形是寫入別的對(duì)象,甚至同時(shí)寫入多個(gè)對(duì)象的數(shù)據(jù)表。
> <field name="value" eval="'product.pricelist,'+str(list0)"/>
前文說過,value字段類型為reference,即形如(product.pricelist,1)的值。這一行就是計(jì)算value的值,形如(product.pricelist,list0),list0(經(jīng)導(dǎo)入后其值是數(shù)字)是引用的product.pricelist中的一條記錄的id,如下。
<record id="list0" model="product.pricelist">
<field name="name">Default Purchase Pricelist</field>
<field name="type">purchase</field>
</record>
也就是說,在<field name="value" eval="'product.pricelist,'+str(list0)"/>之前,必須先有上述導(dǎo)入id="list0"的代碼行。
每個(gè)OpenERP的對(duì)象都有一些預(yù)定義方法,這些方法定義在基類osv.osv中。這些預(yù)定義方法有:
基本方法:create, search, read, browse, write, unlink。
def create(self, cr, uid, vals, context={})
def search(self, cr, uid, args, offset=0, limit=2000)
def read(self, cr, uid, ids, fields=None, context={})
def browse(self, cr, uid, select, offset=0, limit=2000)
def write(self, cr, uid, ids, vals, context={})
def unlink(self, cr, uid, ids)
缺省值存取方法:default_get, default_set。
def default_get(self, cr, uid, fields, form=None, reference=None)
def default_set(self, cr, uid, field, value, for_user=False)
特殊字段操作方法:perm_read, perm_write
def perm_read(self, cr, uid, ids)
def perm_write(self, cr, uid, ids, fields)
字段(fields)和視圖(views)操作方法:fields_get, distinct_field_get, fields_view_get
def fields_get(self, cr, uid, fields = None, context={})
def fields_view_get(self, cr, uid, view_id=None, view_type='form',context={})
def distinct_field_get(self, cr, uid, field, value, args=[], offset=0,limit=2000)
記錄名字存取方法:name_get, name_search
def name_get(self, cr, uid, ids, context={})
def name_search(self, cr, uid, name='', args=[], operator='ilike',context={})
缺省值存取方法:default_get, default_set
def name_get(self, cr, uid, ids, context={})
def name_search(self, cr, uid, name=, args=[], operator=’ilike’, context={})
create方法:在數(shù)據(jù)表中插入一條記錄(或曰新建一個(gè)對(duì)象的resource)。
格式:def create(self, cr, uid, vals, context={})
參數(shù)說明:
vals: 待新建記錄的字段值,是一個(gè)字典,形如: {'name_of_the_field':value, ...}
context (optional): OpenERP幾乎所有的方法都帶有參數(shù)context,context是一個(gè)字典,存放一些上下文值,例如當(dāng)前用戶的信息,包括語言、角色等。context可以塞入任何值,在action定義中,有一個(gè)context屬性,在界面定義時(shí),可以在該屬性中放入任何值,context的最初值通常來自該屬性值。
返回值:新建記錄的id。
舉例:id = pooler.get_pool(cr.dbname).get('res.partner.event').create(cr, uid,{'name': 'Email sent through mass mailing','partner_id': partner.id,'description': 'The Description for Partner Event'})
search方法:查詢符合條件的記錄。
格式:def search(self, cr, uid, args, offset=0, limit=2000)
參數(shù)說明:
args: 包含檢索條件的tuples列表,格式為: [('name_of_the_field', 'operator', value), ...]。可用的operators有:
=, >, <, <=, >=
in
like, ilike
child_of
更詳細(xì)說明,參考《OpenERP應(yīng)用和開發(fā)基礎(chǔ)》中的“域條件”有關(guān)章節(jié)。
· offset (optional): 偏移記錄數(shù),表示不返回檢索結(jié)果的前offset條。
· limit (optional): 返回結(jié)果的最大記錄數(shù)。
返回值:符合條件的記錄的id list。
read方法:返回記錄的指定字段值列表。
格式:def read(self, cr, uid, ids, fields=None, context={})
參數(shù)說明:
· ids: 待讀取的記錄的id列表,形如[1,3,5,...]
· fields (optionnal): 待讀取的字段值,不指定的話,讀取所有字段。
· context (optional): 參見create方法。
返回值:返回讀取結(jié)果的字典列表,形如 [{'name_of_the_field': value, ...}, ...]
browse方法:瀏覽對(duì)象及其關(guān)聯(lián)對(duì)象。從數(shù)據(jù)庫(kù)中讀取指定的記錄,并生成對(duì)象返回。和read等方法不同,本方法不是返回簡(jiǎn)單的記錄,而是返回對(duì)象。返回的對(duì)象可以直接使用"."存取對(duì)象的字段和方法,形如"object.name_of_the_field",關(guān)聯(lián)字段(many2one等),也可以通過關(guān)聯(lián)字段直接訪問“相鄰”對(duì)象。例如:
addr_obj = self.pool.get('res.partner.address').browse(cr, uid, contact_id)
nom = addr_obj.name
compte = addr_obj.partner_id.bank
這段代碼先從對(duì)象池中取得對(duì)象res.partner.address,調(diào)用它的方法browse,取得id=contact_id的對(duì)象,然后直接用"."取得"name"字段以及關(guān)聯(lián)對(duì)象patner的銀行(addr_obj.partner_id.bank)。
格式:def browse(self, cr, uid, select, offset=0, limit=2000)
參數(shù)說明:
select: 待返回的對(duì)象id,可以是一個(gè)id,也可以是一個(gè)id 列表。
· offset (optional): 參見search方法。
· limit (optional): 參見search方法。
返回值:返回對(duì)象或?qū)ο罅斜怼?br />注意:本方法只能在Server上使用,由于效率等原因,不支持rpc等遠(yuǎn)程調(diào)用。
write方法:保存一個(gè)或幾個(gè)記錄的一個(gè)或幾個(gè)字段。
格式:def write(self, cr, uid, ids, vals, context={})
參數(shù)說明:
· ids: 待修改的記錄的id列表。
· vals: 待保存的字段新值,是一個(gè)字典,形如: {'name_of_the_field': value, ...}。
· context (optional): 參見create方法。
返回值:如果沒有異常,返回True,否則拋出異常。
舉例:self.pool.get('sale.order').write(cr, uid, ids, {'state':'cancel'})
unlink方法:刪除一個(gè)或幾個(gè)記錄。
格式:def unlink(self, cr, uid, ids)
參數(shù)說明:
· ids: 待刪除的記錄的id列表。
返回值:如果沒有異常,返回True,否則拋出異常。
default_get方法:復(fù)位一個(gè)或多個(gè)字段的缺省值。
格式: def default_get(self, cr, uid, fields, form=None, reference=None)
參數(shù)說明:
• fields: 希望復(fù)位缺省值的字段列表。
• form (optional): 目前似乎未用(5.06版)。
• reference (optional): 目前似乎未用(5.06版)。
返回值: 字段缺省值,是一個(gè)字典,形如: {'field_name': value, ... }。
舉例:self.pool.get('hr.analytic.timesheet').default_get(cr, uid, ['product_id','product_uom_id'])
default_set方法:重置字段的缺省值。
格式: def default_set(self, cr, uid, field, value, for_user=False)
參數(shù)說明:
• field: 待修改缺省值的字段。
• value: 新的缺省值。
• for_user (optional): 修改是否只對(duì)當(dāng)前用戶有效,還是對(duì)所有用戶有效,缺省值是對(duì)所有用戶有效。
返回值: True
總結(jié)
以上是生活随笔為你收集整理的odoo 的字段。orm对象的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用robotframework做接口测
- 下一篇: ESP32 Eclipse开发环境构建与