This question has been flagged
2 Replies
15965 Views

i have selection field (many2one division field) if i select one division need to load its' employees on the 'notebook section'. how to implement this in openerp 7

Dear Martin, Please check my code @ github from below link.in there 'line 248', when i select division ID need to load workers, whose tagged to relevant division in below notebook section (line 261)

github link

https://github.com/priyankahdp/openerp.git

Avatar
Discard

I don't know about anyone, but I'd need a lot more details before I could understand your question.

Author

@Martin, Post changed. please check & advice me

priyankahdp could you please post code of this? Github address gives 404 not found.

Author

Dear Antanas, Link updated :-)

Thank you.

Best Answer

Use a onchange function for your many2one field which will load the data to the one2many field.

def onchange_many2one_field(cr, uid, ids, many2one_field, context=None):
    value={}
    if many2one_field:
        #find the list of ids that need to be showed in the many2many field and use
        #value.update({'your_many2many_field': ids_you_found}) 
    return {'value':value}

or

You can use the following example to create your onchange function. This is an onchange function I have created for invoice(just an example)

def onchange_partner_id(self, cr, uid, ids, type, partner_id,\
        date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False):
    res = super(account_invoice, self).onchange_partner_id(cr, uid, ids, type, partner_id, 
            date_invoice=date_invoice, payment_term=payment_term, partner_bank_id=partner_bank_id,
            company_id=company_id)

    invoice_lines = []
    product_ids = self.pool.get('product.product').search(cr, uid, [],limit=5)
    for p in self.pool.get('product.product').browse(cr, uid, product_ids):
        invoice_lines.append((0,0,{'product_id':p.id,'name':p.name,
                          'account_id':p.categ_id.property_account_income_categ.id,
                          }))#this dict contain keys which are fields of one2many field 
    res['value']['invoice_line']=invoice_lines
    return res

if there is only single line to be added then res['value'].update({ 'one2many_fieldmname':[(0,0,{'field1':value1,'field2':value2,'fieldn':valuen})] })

In the above example I am searching 5 products and these products are added in the invoiceline. So in your onchange function also you have to search the employee which comes under the division.

In your code,in the form "bpl_work_offer_form" you have add the onchange function at the field bpl_division_id.(in my example partner_id is the many2one field and invoice_line is the one2many field.)

You need to more careful when using onchange to load the one2many field. In the onchange function you have to first unlink all the records loaded in your many2one field. because if once the record is saved then if the onchange is again loaded then a new line will be created. so you need to manage it correctly.

Reference : http://doc.openerp.com/trunk/developers/server/06_misc_on_change_tips/

Avatar
Discard
Author

thanks Omal, how is my function will be.?

answer update. please check it

Author

thanks Omal :-)

this is not working for me it says AttributeError: 'super' object has no attribute 'onchange_planned_qty_id'

post what you have done..its error log and all

Best Answer

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 finally this is the result http://(w)(w)(w). orchidshouseperu.com/screenshots/Captura%20de%20pantalla%20de%202014-03-20%2015:52:20.png

Avatar
Discard