Help

0

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

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 

PACKAGE_TYPE_SELECTION = []

class test(osv.osv) :
_name = "Test"
_columns ={ 
'selection': fields.selection(PACKAGE_TYPE_SELECTION, string='Selection'),
}
Avatar
Discard
4Answers
7

Hello Ali,

You can call a function for your selection field.

Ex:

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.

3Comments
Avatar
Discard

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

No, you cannot do it with onchange.

Avatar
Ưng Tú
-

but function get_journals only run when i update module, pls confirm?

3
Best Answer

Hi,

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.

Avatar
Discard
0
Avatar
Khaled Said
Best Answer
Avatar
Discard
0
Best Answer

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' : 
             {'my_field':
             [('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



Avatar
Discard