Odoo Help


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.


How to access the vals dict.

Tarek Mohamed Ibrahim
on 9/4/15, 10:44 AM 686 views

I created a new record in a one2many field in a view, e.g. the order.line in a sales order screen. I need after each new line added in the detail, the one2many field, to sum the total amount of the order via an onchange method in the qty or price fields. I don't need to go to the 'update' button and click it each time I need to update the total amount. How to do that?


My idea is to access the vals array that is sent to the 'create' method when saving a record, I need to access it before saving.


Axel Mendoza

--Axel Mendoza--
| 6 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 9/4/15, 12:07 PM

The one2many fields store the values in a format described on the write method of the model. For this task you need to pass the one2many field for the on_change method and check the state of their records in order to properly sum your total, something like this:

 def onchange_line_ids(self, cr, uid, ids, order_lines, context=None):  
    total = 0
    line_pool = self.pool.get('sale.order.line')
   for line in order_lines: 
if line[0] == 0:
             total += line[2].get('qty', 0)
        elif line[0] == 4:
            total += line_pool.browse(cr, uid, line[1], context=context).qty
        elif line[0] == 1:
             if line[2].get('qty', False):
                 total += line[2].get('qty', 0)
                 total += line_pool.browse(cr, uid, line[1], context=context).qty  
   return {'value': {'total': total}}

in the onchange method of the price I passed the 'order_id.order_line' and it didn't recognize it. I then passed only the 'order_id' on hope to access 'order_line' in the method, but it gave me what is called 'Newid' and was not able to accomplish my task

Tarek Mohamed Ibrahim
on 9/5/15, 2:04 AM

You cannot do that, you need to put the onchange on the main form level to be able to pass order_line field, you could put the onchange to the order_line field itself because it's the main source of changes that you need to monitor, and pass only order_line instead of order.order_line, in the view you cannot move very easily over relation fields

Axel Mendoza
on 9/5/15, 2:44 AM

thx Axel for answer, it has worked. I accepted the answer and gave you an upvote

Tarek Mohamed Ibrahim
on 9/6/15, 8:08 AM


| 4 3 5
Hyderabad, India

On 9/5/15, 9:00 AM

Hi Tarek, 

If i am wright, you want to sum of total price/qty in your one2many field. you can simply add an attribute "sum" at the qty/price field under tree tag, like:

<tree> [your one2many field tree view]

<field name="[your field]" sum="Total" />



it will show sum of your fields data at the end of tree view.....

Hope it helps!!    

I've already did that and I can see the sum of the amounts every time I do a change in qty or price in any line. My case is different, that is, I have a field that defines the upper limit of a Sales Order amount, this field is per customer, and I read this field in the sale_order class via a function and display it on the order header. When saving the order, the create/write method checks if the sum of the line amounts exceeded this limit, then it raises an exception and stops the save action. The user then have to go to lines and reduce qtys until the amounts sum go below the upper limit allowed. I need this be done automatically via an onchange method. I checked the last reply from Axel and still didn't try it. I think it would work

Tarek Mohamed Ibrahim
on 9/5/15, 10:37 AM

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

2 follower(s)


Asked: 9/4/15, 10:44 AM
Seen: 686 times
Last updated: 9/6/15, 8:07 AM