Skip to Content
Menu
This question has been flagged
5 Replies
11343 Views

Hi,

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.

Avatar
Discard
Best Answer

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

Example:-

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)
Avatar
Discard
Author

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

Best Answer

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: 

https://www.odoo.com/fr_FR/forum/aide-1/question/how-to-prevent-adding-more-than-x-items-into-one2many-98375



Avatar
Discard
Author Best Answer

Hi,

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.

Avatar
Discard
Best Answer

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 !

Avatar
Discard
Related Posts Replies Views Activity
4
Dec 16
14093
1
Oct 23
526
1
Feb 16
6905
1
Mar 15
5926
1
Apr 24
5033