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.

1

How to Use Selection on Many2one Field ?

By
Miftah
on 5/7/15, 4:51 AM 1,475 views

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

2

Temur

--Temur--
2896
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 5/7/15, 6:11 AM

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.

1

Axel Mendoza

--Axel Mendoza--
10238
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 7/10/15, 1:25 PM

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

0

deep

--deep--
1617
| 5 4 7
Bengaluru, India
--deep--

Techno-Functional Associate with 6+ years of experience in Odoo (formerly known as OpenERP).

deep
On 5/7/15, 11:20 AM


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

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

2 follower(s)

Stats

Asked: 5/7/15, 4:51 AM
Seen: 1475 times
Last updated: 7/10/15, 1:27 PM