Odoo Help


Limit the selection in many2many field

Umashankar Subramani
on 3/10/15, 1:12 AM 2,979 views

I am just curious to know whether is it possible to limit number of items to be selected in many2many fields?..... Please anyone give me some suggestion......Thanks in advance.....




| 5 3 8
Pondicherry, India
On 3/10/15, 2:29 AM

Hi Umashankar,

you can not limit the many2many field directly.The way you can limit that make your many2many field as many2one and make it as many2many using widget="many2many" and you can set the limit.for example


<field name="partner_id"  options="{'limit': 10, 'create': false, 'create_edit': false}" widget="many2many">


<field name="your_field1"/>

<field name="your_field2"/>





Ankit H Gandhi(AHG)

--Ankit H Gandhi(AHG)--

| 6 5 6
Baroda, India
--Ankit H Gandhi(AHG)--

Learning Python..

Ankit H Gandhi(AHG)
On 3/10/15, 8:24 AM

Hello Umashankar,

I think this code very helpful for you and this code is fully dynamic.

Here i give one simple example.

in .py file


'field_ids': fields.many2many('other.object.name', 'relation object', 'actual.object.id', 'other.object.id', string='Name'),

def _check_limit(self, cr, uid, ids, context=None):
        new_list = []
        for obj in self.browse(cr, uid, ids, context=context):
            if obj.limit <= 0:
                return False
            for field in obj.field_ids:
            if obj.limit <  len(new_list):
                return False
        return True
    _constraints = [
        (_check_limit, 'Please check your limit !', ['limit','field_ids']),

if you find this answer helpful, please give me a thumbs up vote    

Thanks & Regards,

Ankit H Gandhi

About This Community

This platform 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.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 3/10/15, 1:12 AM
Seen: 2979 times
Last updated: 3/16/15, 8:10 AM