This question has been flagged
2 Replies
15054 Views

Is there any way to access to the items beign added in a list view before saving the form and writing them?

Scenario:

product.product has a o2m relation (packaging field) with product.packaging. If I edit a product in its form view and add a packaging, is there any way to access to all the packaging rows (the ones it had previously and those not saved yet) before saving the product form?

Edit: The moment I need to retrieve the data of the unsaved rows is when adding a new row. I need to set a default value for a field in the new row taking into account the previous packagings the product has, whether they are saved or beign just added.

Thanks in advance

Avatar
Discard
Author

Finally I have found the solution. I share it here for future reference in case someone comes up with the same problem, see answer below.

Best Answer

You can override create method. create method called each time when you save the records.

def create(self, cr, uid, vals, context=None):
    #Your code
    create_id = super(your_class_name, self).create(cr, uid, vals, context=context)
    #Your code
    return create_id

In vals you can find values of all the fields you have filled.


You can override write method. write method called each time when you update the records.

def write(self, cr, uid, ids, vals, context=None):
    #Your code
    res = super(your_class_name, self).write(cr, uid, ids, vals, context=context)
    #Your code
    return res

In vals you can find values of all the fields you are updating.

Avatar
Discard
Author

Sorry, I'll change the question to be more explicit. I meant even before going to save those values. When I add a new row, I need to be "conscious" of the previously added rows, whether they are saved.

Author Best Answer

You can access to all the rows (even those unsaved) when you add a new one passing the one2many field in the context or as a parameter instead of browsing for it using the orm. If the moment you need to access to them is when a field of the form changes, you can follow the instructions shown here:

doc.openerp.com/trunk/developers/server/06_misc_on_change_tips/

In this case you need to pass the o2m field as a paramter. On the other hand, if the moment is when generating the default value for a field of a record beign added, you can pass this information using the context (I had to add the context attribute to an existing view):

<xpath expr="/form/sheet/notebook/page[@string='Sales']/field[@name='packaging']" position="attributes"> <attribute name="context">{'prod_id': id, 'pack': packaging}</attribute> </xpath>

And then retrieve it on the function you defined to calculate the default value:

def _dfl_ean(self, cr, uid, context=None): prod_id = context.get('prod_id',False) packaging = context.get('pack', False)

I hope it helps!

Avatar
Discard