OpenERP在创建父记录时将值传递给子记录
我正在使用OpenERP v7.0开发一个自定义模块,主要是处理工作订单。我想把一个值从父表单传递到子记录,这样子记录就能准备好并通过一个叫many2one的字段与它的父记录关联起来。
我在父记录'work_order.contract'中重写了创建方法,代码大概是这样的:
def create(self, cr, uid, vals, context=None):
if vals.get('contracts_code','/')=='/':
vals['contracts_code'] = self.pool.get('ir.sequence').get(cr, uid, 'work_order.contracts') or '/'
order = super(contracts, self).create(cr, uid, vals, context=context)
"""Creating a contract should be able to create at least one or several
work order records so as to reflect accordingly, but it doesn't link to
the correct contract record that create it.
i.e. contract 3 creates 4 workorders"""
vals['contracts_code'] = order
for count in range(0, vals['work_orders']):
self.pool.get('work_order.work_order').create(cr, uid, {'value' : {'contracts_code': order}}, context)
return order
但是这个重写的方法有点问题:
Create (parent) --------Create (child)
| ^ |
| | |
| | v
| |______________Write (child)
v
Write (parent)
因此,子记录无法和父记录关联,因为父记录的ID还不存在...
我尝试在父记录'work_order.contract'中使用写入方法,代码如下:
def write(self, cr, uid, ids, vals, context=None):
res = super(contracts, self).write(cr, uid, ids, vals, context=context)
"""if there's more than 1 work order in a contract, it should be able
to create the work orders accordingly linked to that contract"""
for x in range(0, vals['work_orders']):
self.pool.get('work_order.work_order').create(cr, uid, {'value' : {'contracts_code': vals['contracts_code']}}, context)
return res
这个重写的方法看起来像这样:
Create (parent)
|
v
Write (parent) --------> Create (child)
^ |
|_____________Write (child)
但是在调用子记录创建的时候,原本需要传递的值却在中间消失了。
在提问的时候,我意识到传递的数据是字符类型,而不是像ID那样的整数类型,但我不确定如何传递正确的ID,因为在创建过程中它还不存在。
如果我做错了,请告诉我另一种准备子记录的方法,以便知道它的父记录。谢谢!=)
1 个回答
2
在父类中:
def create(self, cr, uid, values, context=None):
.
.
# prep everything for parent record
.
.
new_id = super(parent_class, self).create(cr, uid, values, context=context)
# at this point, you have the id of the new record
.
# prep whatever you need for child record(s)
.
child_values = {'parent_id':new_id, ... }
child_class.create(cr, uid, child_values, context=context)
这就是如何让新的父级ID在子记录中可用的方法。