Odoo Help


How to use selection on many2one?

René Schuster
on 3/13/14, 8:09 AM 4,628 views

As described here, I have created a many2one field with custom selection function:

def _buyer_selection(self, cr, uid, context=None):
    partner_obj = self.pool.get('res.partner');
    res = [];
    partner_ids = partner_obj.search(cr, uid, [('buyer','=',True)]);
    for partner in partner_obj.browse(cr, uid, partner_ids, context=context):
        res.append((partner.id, partner.buyer_code))
    return res;

_columns = {
    'buyer_id': fields.many2one('res.partner', 'Buyer', selection=_buyer_selection),

But in the view

    <field name="buyer_id"/>

It still acts like a normal many2one field. All partners selectable, no buyer_code displayed.


The debug view shows a perfectly working selection...

What am I missing?!


 <field name="buyer_id" widget="selection"/>

Selection widget doesn't work.
The field will become a selection field indeed, but the custom selection function seems to be overridden:


I think based on your requirement to override name_get method and shows buyer_code

on 3/13/14, 8:43 AM

Thanks for your reply.

I only need the buyer_code in this single case. All other modules should show the partner.name.

René Schuster
on 3/13/14, 8:52 AM

For your situation i think you should use fields.related

Sajad KK
on 3/13/14, 8:57 AM

René Schuster

--René Schuster--
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 3/17/14, 7:50 AM

Solved as prakash suggested.

Overriding name_get

def name_get(self, cr, uid, ids, context=None):
    if context.get('view_buyer_code', False):            
        res = [];
        for partner in self.browse(cr, uid, ids, context=context):
            res.append((partner.id, partner.buyer_code));
        res = super(res_partner_x, self).name_get(cr, uid, ids, context=context);
    return res;

And using context to control the viewed name

<field name="buyer_id" domain="[('buyer','=',True)]" context="{'view_buyer_code': True}"></field>

Only Disadvantage: After selectiong the Buyer the normal Partner name is displayed.
But I guess I can live with that.




| 5 5 7
Bikaner, India

OpenERP Developer at SunARC Technology

On 3/13/14, 8:28 AM

In xml file many2one field add widget="selection"

<field name="buyer_id" widget="selection"/>

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/13/14, 8:09 AM
Seen: 4628 times
Last updated: 3/16/15, 8:10 AM