Skip to Content
Menu
This question has been flagged

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
Best Answer

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.

Avatar
Discard
Author

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

No, you cannot do it with onchange.

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

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
Best Answer

This post has years but I had the same question. May this way can help for new implementations


type_tax_use = fields.Selection(
[
('sale', 'Sale'),
​('purchase', 'Purchase'),
​], default='purchase', string="Tax Type")

move_type = fields.Selection(
[
('in_invoice', 'Purchase Invoice'),
('in_refund', 'Purchase Credit Note'),
('out_invoice', 'Sale Invoice'),
('out_refund', 'Sale Credit Note'),
], default='in_invoice', string="Invoice Type"
)

sale_move_type = fields.Selection(
[
('out_invoice', 'Sale Invoice'),
('out_refund', 'Sale Credit Note'),
], default='out_invoice', string="Sale Invoice Type"
)
purchase_move_type = fields.Selection(
[
('in_invoice', 'Purchase Invoice'),
('in_refund', 'Purchase Credit Note'),
], default='in_invoice', string="Purchase Invoice Type"
)

@api.onchange('sale_move_type','purchase_move_type')
def _onchange_ref_move_type(self):
if self.type_tax_use == 'sale':
self.move_type = self.sale_move_type
elif self.type_tax_use == 'purchase':
self.move_type = self.purchase_move_type

In the xml you show the desired field according to type_tax_use with domain scope

Avatar
Discard
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
Related Posts Replies Views Activity
0
Dec 15
6324
5
Nov 15
5278
1
Jul 15
7147
0
Oct 17
8887
1
Jan 17
4846