夏肇毅知識運算網-基礎OpenERP(Odoo)資料庫物件關聯對應(ORM)教學
2016/05/07 00:17
瀏覽615
迴響0
推薦2
引用0
[夏肇毅知識運算網 Mark Hsia's Knowledge Computing Web]
基礎OpenERP(Odoo)資料庫物件關聯對應(ORM)教學
2016-05-04
Basic Tutorial Samples of Odoo Object Relational Mapping(ORM)
The traditional Odoo Object Relational Mapping(ORM) API passes the database cursor, user id, context dictionary and record ids(usually denoted ascr, uid, context, ids) to all methods. For example:self.browse(cr, uid, sids, context=context)
These parameters have been removed from the new Record style API since Odoo 8. But the programs of Odoo are written in traditional style. So it is still very important to understand how it works.
The basic ORM method is search(). We can provide some searching criteria (domain) to the method like [('uuid', '=', uuid)] to screen out the records we want. And the browse() returns the records we need according the input ids parameter.
Here is the sample of search() and browse():
// Sample of reading own records: search(),browse() directly
def is_in_session(self, cr, uid, uuid, user_id, context=None):
""" return if the given user_id is in the session """
sids = self.search(cr, uid, [('uuid', '=', uuid)], context=context, limit=1)
for session in self.browse(cr, uid, sids, context=context):
return user_id and user_id in [u.id for u in session.user_ids]
return False
""" return if the given user_id is in the session """
sids = self.search(cr, uid, [('uuid', '=', uuid)], context=context, limit=1)
for session in self.browse(cr, uid, sids, context=context):
return user_id and user_id in [u.id for u in session.user_ids]
return False
If we need to access the data of another class, we should use the pool[] to access that class first.
// Sample of Reading records of other class: use pool[] first def users_infos(self, cr, uid, ids, context=None):
""" get the user infos for all the user in the session """
for session in self.pool["im_chat.session"].browse(cr, uid, ids, context=context):
users_infos = self.pool["res.users"].read(cr, uid, [u.id for u in session.user_ids], ['id','name', 'im_status'], context=context)
return users_infos
Another useful method is search_read(). It is a combination of a search() and a read(). We need to prepare the domain for search() and the fields list for read(). Look at this example:
// Sample of Odoo ORM search_read(): Performs a search() followed by a read()
def session_info(self, cr, uid, ids, context=None):
""" get the session info/header of a given session """
for session in self.browse(cr, uid, ids, context=context):
info = {
'uuid': session.uuid,
'users': session.users_infos(),
'state': 'open',
}
# add uid_state if available
if uid:
domain = [('user_id','=',uid), ('session_id','=',session.id)]
uid_state = self.pool['im_chat.conversation_state'].search_read(cr, uid, domain, ['state'], context=context)
if uid_state:
info['state'] = uid_state[0]['state']
return info
""" get the session info/header of a given session """
for session in self.browse(cr, uid, ids, context=context):
info = {
'uuid': session.uuid,
'users': session.users_infos(),
'state': 'open',
}
# add uid_state if available
if uid:
domain = [('user_id','=',uid), ('session_id','=',session.id)]
uid_state = self.pool['im_chat.conversation_state'].search_read(cr, uid, domain, ['state'], context=context)
if uid_state:
info['state'] = uid_state[0]['state']
return info
The last sample of this tutorial is write(). Just use it to write data back to the records.
// Sample of Odoo ORM write:
def update_state(self, cr, uid, uuid, state=None, context=None):""" modify the fold_state of the given session, and broadcast to himself (e.i. : to sync multiple tabs) """
domain = [('user_id','=',uid), ('session_id.uuid','=',uuid)]
ids = self.pool['im_chat.conversation_state'].search(cr, uid, domain, context=context)
for sr in self.pool['im_chat.conversation_state'].browse(cr, uid, ids, context=context):
if not state:
state = sr.state
if sr.state == 'open':
state = 'folded'
else:
state = 'open'
self.pool['im_chat.conversation_state'].write(cr, uid, ids, {'state': state}, context=context)
self.pool['bus.bus'].sendone(cr, uid, (cr.dbname, 'im_chat.session', uid), sr.session_id.session_info())
自訂分類:雲端運算
上一則: 夏肇毅知識運算網-在免費Odoo開源ERP(OpenERP)銷售系統上新建一個公司下一則: 夏肇毅知識運算網-在Ububtu Linux 上安裝 Odoo 免費OpenERP軟體的體驗
你可能會有興趣的文章:
限會員,要發表迴響,請先登入












