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

How to use selection on many2one?

By
René Schuster
on 3/13/14, 8:09 AM 2,595 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

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

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

René Schuster

--René Schuster--
1560
| 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));
    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.

0

Prakash

--Prakash--
3173
| 5 4 7
Bikaner, India
--Prakash--

OpenERP Devloper at SunARC Technology

Prakash
On 3/13/14, 8:28 AM

In xml file many2one field add widget="selection"

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

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