Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
2 Antwoorden
14548 Weergaven

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

Avatar
Annuleer
Beste antwoord

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>

Avatar
Annuleer
Beste antwoord

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

Avatar
Annuleer