This question has been flagged
2 Replies
12700 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.
Although:

debug_many2one

The debug view shows a perfectly working selection...

What am I missing?!

EDIT:

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

debug_many2one_2

Avatar
Discard

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

Author

Thanks for your reply.

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

For your situation i think you should use fields.related

Author Best Answer

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));
    else:
        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.

Avatar
Discard
Best Answer

In xml file many2one field add widget="selection"

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