我正在使用OpenERP v7.0开发OpenERP自定义模块工作单。我一直在尝试将父表单中的值传递给子记录,以便准备子记录并通过many2one字段链接到其父记录。
我在父级'work_order.contract'中重写了create方法:
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)
并因此不会将孩子与其父母联系起来,因为父母的身份仍未存在......
我尝试在父'work_order.contract'中使用write方法:
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)
但不知何故,在调用孩子创建记录的过程中,需要传递的值不知何故消失了。
在提出这个问题时,只是意识到传递的数据是char而不是像id这样的整数,但是我不知道如何传递正确的id,因为它尚未存在(在创建过程中)。 / p>
如果我做错了,请告诉我另一种准备儿童记录的方法,以便了解其父记录。谢谢=)
答案 0 :(得分: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在子记录中可用。