Odoo Help


How to create dynamic selection field (fields.selection)?

Ali Abdelmonem
on 3/17/16, 7:27 AM 3,177 views

i face a problem with filling the (fields.selection) with dynamic data comes from database 

so for example if i change the customer value i want to fill the selection field with the coming data from database dynamically

i just need to do this with the selection field


from openerp.osv import osv,fields 


class test(osv.osv) :
_name = "Test"
_columns ={ 
'selection': fields.selection(PACKAGE_TYPE_SELECTION, string='Selection'),

Sudhir Arya (SA)

--Sudhir Arya (SA)--
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow




Sudhir Arya (SA)
On 3/17/16, 2:29 PM

Hello Ali,

You can call a function for your selection field.


class ...
def get_journals(cr, uid, context=None):
journal_obj = self.pool.get('account.journal')
journal_ids = journal_obj.search(cr, uid, [], context=context)
lst = []
for journal in journal_obj.browse(cr, uid, journal_ids, context=context):
lst.append((journal.id, journal.name))
return lst
    _columns ={ 
        'selection': fields.selection(_get_journals, string='Selection'),

Hope this will help you.

Thanks A lot for your reply but if i want to do it via onchange method ?

Ali Abdelmonem
on 3/20/16, 6:30 AM

No, you cannot do it with onchange.

Sudhir Arya (SA)
on 3/25/16, 12:21 AM

Ankit H Gandhi(AHG)

--Ankit H Gandhi(AHG)--
Ankit H Gandhi(AHG)
| 5 3 6
Rajkot, India
--Ankit H Gandhi(AHG)--

Learning Python..

Ankit H Gandhi(AHG)
On 3/18/16, 3:57 AM


You can see this type of custom dropdownlist (selection) into pricelist file of product module. It looks like below.

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

mf = self.pool.get('ir.model.fields')

ids = mf.search(cr, uid, [('model','in', (('product.product'),('product.template'))), ('ttype','=','float')], context=context)

res = []

for field in mf.browse(cr, uid, ids, context=context):

res.append((field.name, field.field_description))

return res

_columns = {

"field" : fields.selection(_price_field_get, "Product Field", size=32, required=True, help="Associated field in the product form."),


Here 'field' is selection and its data is comes dynamic from the method "_price_field_get".

Hope you will get custom dropdownlist.

Best Regards,

Ankit H Gandhi.


Tarek Mohamed Ibrahim

--Tarek Mohamed Ibrahim--
Tarek Mohamed Ibrahim
| 5 3 7
Gîza, Egypt
--Tarek Mohamed Ibrahim--

I am an old VFP developer on ERP

I have moved to 2p since Nov-2014 and started developing with Python on Odoo.



Tarek Mohamed Ibrahim
On 3/26/16, 8:18 AM

Make your field a many2one field

'my_field': fields.many2one('my.table', ... ),

put the 'selection' widget

<field name="my_field" widget="selection" ...>

in an 'on_change' method set the appropriate domain for your selection field and return this domain , this will change the values appearing in your dropdown selection list

def onchange_for_my_field(self, cr, uid, ids, param1, param2, ... , context=None):
       return {'domain' : 
             [('field1','=',val1),('field2','=',val2),... ] }

where field1 and field2 , ... are fields in the table 'my.table' that could be used to filter the result.

Hope this solution is useful

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 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/17/16, 7:27 AM
Seen: 3177 times
Last updated: 3/26/16, 8:18 AM