Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
14546 Vizualizări

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

Imagine profil
Abandonează
Cel mai bun răspuns

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

<group>

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

<tree>

<field name="your_field1"/>

<field name="your_field2"/>

</tree>

</field>

</group>

Imagine profil
Abandonează
Cel mai bun răspuns

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

'limit':fields.integer('Limit'),

'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:
                new_list.append(field.id)
            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

Imagine profil
Abandonează