Question

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 =)

Was it helpful?

Solution

In the parent class:

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)

And that is how you get the new parent id to be available in the child record.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top