python - How to load one to many children on onchange event? -
i have selection field (many 1 division field) if select 1 division need load its' employees on 'notebook section'. how implement in openerp 7?
if selected relevant estate id need load relevant divisions.if division select need load relevant workers in following phase
<record model="ir.ui.view" id="bpl_work_offer_form"> <field name="name">bpl.work.offer.form</field> <field name="model">bpl.work.offer</field> <field name="arch" type="xml"> <form string='bpl_work_offer' version='7.0'> <sheet> <group> <group> <field name='bpl_estate_id' /> <field name='bpl_division_id' /> <field name='user_id' /> </group> <group> <field name='date_of_offer' /> <field name='payment_type' /> <field name='select_by' /> <field name='no_of_workers' attrs="{'invisible':[('select_by','=','by_name')]}" /> <field name='work_type' /> </group> </group> <div name="worker selection"></div> <separator string='select workers' /> <notebook attrs="{'invisible':[('select_by','=','by_count')]}"> <page string="tea workers" attrs="{'readonly':[('work_type','!=','tea')]}"> <field name='selected_tea_workers_line_ids' nolabel='1'> <tree string='list' editable='bottom'> <field name='worker_id' /> <field name='is_selected' /> </tree> </field> <group class='oe_subtotal_footer oe_right'> <field name='total_workers' /> </group> <br /> <br /> </page> <page string="rubber workers" attrs="{'readonly':[('work_type','!=','rubber')]}"> <field name='selected_rubber_workers_line_ids' nolabel='1'> <tree string='list' editable='bottom'> <field name='worker_id' /> <field name='is_selected' /> </tree> </field> <group class='oe_subtotal_footer oe_right'> <field name='total_workers' /> </group> <br /> <br /> </page> <page string="sundry workers" attrs="{'readonly':[('work_type','!=','sundry')]}"> <field name='selected_sundry_workers_line_ids' nolabel='1'> <tree string='list' editable='bottom'> <field name='worker_id' /> <field name='is_selected' /> </tree> </field> <group class='oe_subtotal_footer oe_right'> <field name='total_workers' /> </group> <br /> <br /> </page> </notebook> </sheet> </form> </field> </record> <record model="ir.actions.act_window" id="bpl_work_offer_action"> <field name="name">bpl.work.offer.action</field> <field name="res_model">bpl.work.offer</field> <field name="view_type">form</field> <field name="view_mode">form</field> </record>
model code
class company_new_registration(osv.osv): _name = "bpl.company.n.registration" _description = "company" _columns = { 'name': fields.char('company name', size=128, required=true), 'estates': fields.one2many('bpl.estate.n.registration', 'company_id', 'estate') } # bill of material company_new_registration() class estate_new_registration(osv.osv): _name = "bpl.estate.n.registration" _description = "estates" _columns = { 'name': fields.char('estate name', size=128, required=true), 'company_id': fields.many2one('bpl.company.n.registration', 'company name', select=true), 'divisions': fields.one2many('bpl.division.n.registration', 'estate_id', 'division') } estate_new_registration() class division_new_registration(osv.osv): _name = "bpl.division.n.registration" _description = "divisions" _columns = { 'name': fields.char('division name', size=128, required=true), 'estate_id': fields.many2one('bpl.estate.n.registration', 'estate name', select=true), } division_new_registration()
and needed mapping work offer (plantation project)
class bpl_work_offer(osv.osv): _name = "bpl.work.offer" _description = "bpl work offer" _columns = { 'user_id': fields.many2one('res.users', 'user name'), 'date_of_offer': fields.date('date'), 'bpl_estate_id':fields.many2one('bpl.estate.n.registration', 'estate', help='estate'), 'bpl_division_id':fields.many2one('bpl.division.n.registration', 'division', help='division'), 'payment_type': fields.selection([('normal_work', 'normal work'), ('cash_work', 'cash work')], 'payment type'), 'select_by': fields.selection([('by_name', 'by names'), ('by_count', 'by count')], 'select by'), 'no_of_workers': fields.integer('no of workers'), 'work_type': fields.selection([('tea', 'tea'), ('rubber', 'rubber'), ('sundry', 'sundry')], 'work type'), 'total_workers': fields.integer('total workers'), 'selected_tea_workers_line_ids': fields.one2many('bpl.selected.tea.workers.line', 'worker_id', 'tea workers', ondelete="cascade"), 'selected_rubber_workers_line_ids': fields.one2many('bpl.selected.rubber.workers.line', 'worker_id', 'rubber workers', ondelete="cascade"), 'selected_sundry_workers_line_ids': fields.one2many('bpl.selected.sundry.workers.line', 'worker_id', 'sundry workers', ondelete="cascade"), } bpl_work_offer() class selected_tea_workers_line_ids(osv.osv): _name = 'bpl.selected.tea.workers.line' _description = 'bpl selected tea workers line' _columns = { 'worker_id':fields.many2one('bpl.worker', 'tea worker', ondelete='cascade', help='worker'), 'is_selected': fields.boolean('select', help="selected or not"), } selected_tea_workers_line_ids() class selected_rubber_workers_line_ids(osv.osv): _name = 'bpl.selected.rubber.workers.line' _description = 'bpl selected rubber workers line' _columns = { 'worker_id':fields.many2one('bpl.worker', 'rubber worker', ondelete='cascade', help='worker'), 'is_selected': fields.boolean('select', help="selected or not"), } selected_rubber_workers_line_ids() class selected_sundry_workers_line_ids(osv.osv): _name = 'bpl.selected.sundry.workers.line' _description = 'bpl selected sundry workers line' _columns = { 'worker_id':fields.many2one('bpl.worker', 'sundry worker', ondelete='cascade', help='worker'), 'is_selected': fields.boolean('select', help="selected or not"), } selected_sundry_workers_line_ids()
you can use following example create onchange function. onchange function have created invoice(just 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) 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 fields of one2many field res['value']['invoice_line']=invoice_lines return res
if there single line added res['value'].update({ 'one2many_fieldmname':[(0,0,{'field1':value1,'field2':value2,'fieldn':valuen})] })
in above example searching 5 products , these products added in invoiceline. in onchange function have search employee comes under division.
in code,in form "bpl_work_offer_form" have add onchange function @ field bpl_division_id.(in example partner_id many2one field , invoice_line one2many field.)
you need more careful when using onchange load one2many field. in onchange function have first unlink records loaded in many2one field. because if once record saved if onchange again loaded new line created. need manage correctly.
reference : http://doc.openerp.com/trunk/developers/server/06_misc_on_change_tips/
Comments
Post a Comment