Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
5 Trả lời
71632 Lượt xem

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'),
}
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Tác giả

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?

Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

there are 3 pattern for that to be approached

https://dev.to/kerbrose/dynamic-selection-list-in-odoo-for-selection-fields-16h8

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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



Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 12 15
6803
5
thg 11 15
5682
1
thg 7 15
7526
0
thg 10 17
9459
1
thg 1 17
5382