Odoo Help

Welcome!

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.

0

Limit the selection in many2many field

By
Umashankar Subramani
on 3/10/15, 1:12 AM 1,550 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.....

1

Vasanth

--Vasanth--
3458
| 5 3 8
Pondicherry, India
--Vasanth--
Vasanth
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

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

0

Ankit H Gandhi(AHG)

--Ankit H Gandhi(AHG)--
996
| 5 3 6
Rajkot, 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

'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

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

1 follower(s)

Stats

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