I'm working on an OpenERP custom module work order using OpenERP v7.0. I've been trying to pass a value from parent form to a child record so that the child record is prepared and linked to its parent via many2one field.
I've override the create method as so in parent '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
but this overriding method is somewhat like:
Create (parent) --------Create (child)
| ^ |
| | |
| | v
| |______________Write (child)
v
Write (parent)
and therefore wouldn't link the child to its parent as parent's id is still yet to be existed...
I've tried using write method in parent '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
and this overriding method look something like:
Create (parent)
|
v
Write (parent) --------> Create (child)
^ |
|_____________Write (child)
but somehow the value that needed to pass is somehow gone in the middle of calling the child to create the record.
Well in making this question, just realize that the data passed is char and not integer like id, but then I'm not sure how to pass the right id as it's not yet existed (in the middle of creation).
If what I did is wrong, please show me another way to prepare the child record so as to know its parent record. Thanks =)