How to get OpenERP to execute a function when it loads the view | Odoo

How to get OpenERP to execute a function when it loads the view

I want to execute a function that will get executed when when the view is loaded and populate a dropdown list box.

How can I do this. I have already defined the function in .py file.

Any ideas please.

Yogesh Vachhani
Yogesh Vachhani
70
| 4 1 3
Asked on 5/8/13, 5:21 PM
2
vote
1526 Views

but the dropdown manu is an openerp field?

Francesco OpenCode
on 5/8/13, 6:23 PM

Yes it is an OpenERP field.

Merlin TecSol Pvt. Ltd., Yogesh Vachhani
on 5/10/13, 8:40 AM

So, you want to create a selection with a function?

Francesco OpenCode
on 5/10/13, 11:38 AM

Hi,

create a field type selection with a function which will create the list you need in you dropbox list (here in this example, the list of result is Month and Year in list of unit of measure, change the code for your own need):

def _sel_func(self, cr, uid, context=None):
    obj = self.pool.get('product.uom')
    ids = obj.search(cr, uid, [])
    res = obj.read(cr, uid, ids, ['name', 'id'], context)
    res = [(r['id'], r['name'])  for r in res if r['name'] == _("Month") or r['name'] == _("Year")
    return res

_columns = {
    'field_name': fields.selection(_sel_func, string='Field name'),
}

Bye

Cyril Gaspard (GEM)
Cyril Gaspard (GEM)
2638
| 4 6 8
Answered On 5/11/13, 4:42 PM
4
vote

Yeah, thanks, this solutions works for me. i do that:

define in my class:

class stock_picking(osv.osv):
    _name = 'stock.picking' 
    _inherit = 'stock.picking'
    _columns = {
     'aux_almacen_orig': fields.selection(_buscar_shortname_alm,method="True", type="char", size=256, string="Almacen Origen" ),

then i define the function this way:

def _buscar_shortname_alm(self, cr, uid, context=None):
 obj = self.pool.get('stock.location')
        ids = obj.search(cr, uid, [])
        res = obj.read(cr, uid, ids, ['shortcut', 'id'], context)
        res = [(r['id'], r['shortcut']) for r in res if r['shortcut'] != False]
        return res

note: i define shorcut in my class stock location, this field only for to short the complete name in mi case, but you can change with another field that you have in your class:

class stock_location(osv.osv):
    _name = "stock.location"
    _inherit = "stock.location"
_columns = {
        'shortcut' :  fields.char('Nombre Corto',size=50),

and the XML file

<field name="aux_almacen_orig" />

and finally this is the result http://(w)(w)(w). orchidshouseperu.com/screenshots/Captura%20de%20pantalla%20de%202014-03-20%2015:52:20.png

wilson zauma
wilson zauma
37
| 3 1 2
Answered On 3/20/14, 11:03 PM
1
vote

Hi,

define the function in defaults section of py file for your field, you have an example in account/account.py :

_defaults = {
    'cparent_id': _get_def_cparent,
}

Bye

Cyril Gaspard (GEM)
Cyril Gaspard (GEM)
2638
| 4 6 8
Answered On 5/11/13, 3:41 PM
0
vote

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.

Ask a Question

About This Forum

This community is for professionals and enthusiasts of our products and services.


Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 5/8/13, 5:21 PM
Seen: 1526 times
Last updated: 3/20/14, 11:55 PM