Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How can I get data from the rows being created in a list view before saving?

By
Pablo
on 4/5/13, 9:16 AM 3,773 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

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.

Pablo
on 4/8/13, 7:25 AM
0

Sudhir Arya (SA)

--Sudhir Arya (SA)--
10150
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow

LinkedIn

Blog

Stackoverflow

Sudhir Arya (SA)
On 4/5/13, 9:25 AM

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.

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.

Pablo
on 4/5/13, 10:11 AM
0
Pablo
On 4/8/13, 7:46 AM

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!

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 4/5/13, 9:16 AM
Seen: 3773 times
Last updated: 3/16/15, 8:10 AM