Help

3

How to Use Selection on Many2one Field ?

As describe in https://doc.odoo.com/6.0/developer/2_5_Objects_Fields_Methods/field_type/#relational-types

I have created a many2one field with custom selection function :

def _get_partner_sup(self, cr, uid, context=None):
    obj = self.pool.get('res.partner')
    ids = obj.search(cr, uid, [('supplier','=',True), ('is_company','=',True)])
    res = obj.read(cr, uid, ids, ['name', 'id','email'], context)
    res = [(r['id'], r['name']) for r in res]    
    return res

_columns = {
    'partner_sup_id': fields.many2one('res.partner', 'Select Supplier 2 ', selection=_get_partner_sup),
}

But it's still like a normal many2one field. What is wrong ?

And I don't want use widget='selection' or domain=[('supplier','=',True), ('is_company','=',True)]

Thanks in Advance

Avatar
Discard
3 Answers
2
Avatar
Temur
Best Answer

Wrong parameter. The many2one field doesn't have parameter "selection". 

Can you explain please, why you don't want to use:

_columns = {
'partner_sup_id': fields.many2one('res.partner', 'Select Supplier 2 ', domain=[('supplier','=',True), ('is_company','=',True)),
}
you can also set domain in the XML, if you like to have domain filter only in the particular page.

?

if you want to limit displayed columns(fields) to the 'name' field in the selection(you seem to try this in your function), then you've to achieve it with custom view (treeview) for this model.

Avatar
Discard
1
Avatar
Axel Mendoza
Best Answer

You can use any of the provided Options here but the final adjustment is that you need to set the widget selection for the field in your xml view definition. For example:

#in your .py  

_columns = {

    'partner_sup_id': fields.many2one('res.partner', 'Select Supplier 2 ', domain=[('supplier','=',True), ('is_company','=',True)),

}

 

#in your xml
 

<field name="partner_sup_id" widget="selection">
 

Avatar
Discard
0
Avatar
deep
Best Answer


Well if you don't want to use Domain / Widget...

Then you can try with either of these options

Option 1:

Define a field as Selection, instead of Many2One, along with a selection to achieve your requirement...

Coming to your Requirement:

def _get_partner_sup(self, cr, uid, context=None):

obj = self.pool.get('res.partner')

ids = obj.search(cr, uid, [('supplier','=',True), ('is_company','=',True)])

res = obj.read(cr, uid, ids, ['name', 'id','email'], context)

res = [(r['id'], r['name']) for r in res]

return res

_columns = {

'partner_sup_id': fields.Selection(selection=_get_partner_sup),

}

Option2: If your intention is just not to provide Create/Edit option to the users, then in XML, against that fields just "options={'no_create':True}", by doing this also, you can achieve the same...

Avatar
Discard