Odoo Help

0

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

By
Ali Abdelmonem
on 3/17/16, 7:27 AM 4,193 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 

PACKAGE_TYPE_SELECTION = []

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

Sudhir Arya (SA)

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

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow

LinkedIn

Blog

Stackoverflow

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

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.

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
2

Ankit H Gandhi(AHG)

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

Learning Python..

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

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.

0

Tarek Mohamed Ibrahim

--Tarek Mohamed Ibrahim--
904
| 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.

https://www.linkedin.com/profile/public-profile-settings?trk=prof-edit-edit-public_profile

tibrahim@2p.com.sa

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' : 
             {'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



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.

Register

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)

Stats

Asked: 3/17/16, 7:27 AM
Seen: 4193 times
Last updated: 3/26/16, 8:18 AM