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 limit the total number values for a one2many field?

on 4/15/14, 7:38 AM 2,347 views


I would like to have a feature on the one2many field. Usually we can add any number of values to a one2many field. But I want restrict the user to enter only the first three values and not more than three. Three is just an example count. I should be able to define it to any number.

To be more clear, I want the user to enter only 3 line items in the sales order. After entering 3 line items, the create/add option should disappear.

Is there any way already existing? or is it to be implemented yet?

Thanks in advance.



| 5 4 7
Bikaner, India

OpenERP Devloper at SunARC Technology

On 4/15/14, 8:18 AM

I have solution but without disappear Create/add option. To override create and write method and allow to user enter only 3 lines item.


def create(self, cr, uid, vals, context=None):
       if vals.get('order_line'):
            count = len(vals.get('order_line'))
            if count > 3:
                raise osv.except_osv(_('Warning!'), _('Limit to create 3 Lines'))
        return super(sale_order, self).create(cr, uid, vals, context=context)

def write(self, cr, uid, id, vals, context=None):
    if vals.get('order_line'):
        count = len(vals.get('order_line'))
        if count > 3:
            raise osv.except_osv(_('Warning!'), _('Limit to create 3 Lines'))
    return super(sale_order, self).write(cr, uid, id, vals, context=context)

Prakash, that works but a little late while saving the form. But It should work instantly as we try to add the fourth one. Because, i will be able to add more than 3 upto any number with your solution and it is going to warn me only at the moment of saving the form. my time of entering more than 3 should be saved right upfront. I think it is only possible if we make changes in the x2many widget java scripts

on 4/15/14, 8:36 AM
Yug Faa
On 4/15/14, 9:35 AM

You will find a addons here :

bzr branch lp:~sylvain-legal/web-addons/web_field_float_compute

Module named : web_m2x_options

Make it resolved !

Ameen Rabea
On 2/28/16, 10:52 AM

I posted similar question of yours, but I need when user add more than , let's say, 5 items I warn him and delete the extra elements.

I was able to do onchange method, but I am not able to delete the extra items he added. Also I don't need to wait until the user click the parent "Save" button, but I need once he added the extra child element I delete it before waiting until save.

I tried to delete the extra item from child_ids list, or to use the unlink function but of Parent and Child, but no luck =(

How to delete the extra elements?

here is my post: 


On 4/26/14, 4:55 AM


I have found a solution that best suits my requirement. That is to have a on_change method for the x2many field and check the count using len() python function. If it is more than 3 or x number, then popup a error message.

Apart from these, there are two other solutions. they have been mentioned already by Yug Faa and Prakash. i.e

1) Customize the x2many widget using js

2) Inherit the create and write method

Thanks to Prakash and Yug for their contribution.

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)


Asked: 4/15/14, 7:38 AM
Seen: 2347 times
Last updated: 2/28/16, 10:52 AM