Arsalan Sherwani
Arsalan Sherwani

Reputation: 560

Make new One2many record through Wizard Odoo

I want to ask that how can I able to create One2many records through my wizard? Like I have a parent class A. On that form I have Created a One2many field. A button is placed(on parent form) to open up a wizard. The wizard has some selection fields. Now when I set the selection fields and click button (on wizard), I want to have all the result of my filtered records created on the One2many on the parent form. I have no idea how to get this done. My values are returning from wizard to parent form (simple fields) but I don't know how to get this done when dealing with One2many fields. Below is my parent class with the One2many relation class:

from openerp import models, fields, api
import openerp.exceptions

class hr_winter_module(models.Model):
    _name = "hr.winter.module"
    eligible_winter_ids = fields.One2many('hr.employee.winter.module', 'hr_winter_module_id', 'For Employee')
    set_amount = fields.Float('VALUE')
    my_name = fields.Char('NAME')
    my_job = fields.Char('JOB')
    my_country = fields.Char('COUNTRY')
    proposal_date = fields.Date('Date of Proposal', required=True, default=fields.Date.today() )
    _rec_name="proposal_date"


class hr_employee_winter_module(models.Model):

    _name='hr.employee.winter.module'

    @api.onchange('emp_name')
    @api.depends('emp_name')
    def myempl_name(self):
        for myrec in self:
            contract_records = myrec.env['hr.contract'].search([('employee_id','=', myrec.emp_name.id),('date_start','<=',fields.Date.today()),'|',('date_end','=',False),('date_end','>=',fields.Date.today())])
            stable=False
            if contract_records:
                stable = contract_records[-1:].analytic_account_id.name
            myrec.emp_stable= stable


    hr_winter_module_id = fields.Many2one("hr.winter.module", "Employee", ondelete="cascade")
    win_emp_name = fields.Many2one('hr.employee', string='Employee Name')
    win_zeo_number = fields.Char(related='win_emp_name.zeo_number', string='ZEO Number', readonly=True )

This is my wizard code :

from openerp import models, fields, api
import openerp.exceptions

class wizard_model(models.TransientModel):
    _name = "wizard.model"

    employee_type = fields.Selection([('muslim','Muslim'),('non-muslim','Non-Muslim'),('all','All')], default='all')
    stable_id = fields.Many2many('account.analytic.account', 'employee_stable', 'id', 'analytic_account_id', 'Stables', domain=[('parent_id','=','Stables') ])
    amount_for_all = fields.Float('Amount')
    emp_name = fields.Char('Enter Name')
    emp_job = fields.Char('Enter Job')
    emp_country = fields.Char('Enter Nationalty')
    @api.one
    def set_val(self):
        ids=self._ids
        cr = self._cr
        uid = self._uid
        context = self._context
        model_name=context.get('active_model')
        form_id = context.get('active_id')
        obj = self.pool.get(model_name).browse(cr,uid,form_id,context)

        obj.set_amount=self.amount_for_all
        obj.my_name = self.emp_name
        obj.my_job = self.emp_job
        obj.my_country = self.emp_country                  
        self.pool.get('hr.winter.module').write(cr, uid, ids, {'eligible_winter_ids':[ (0, 0, {'win_emp_name':self.emp_name})]})

Upvotes: 1

Views: 7560

Answers (1)

shravan cherukuri
shravan cherukuri

Reputation: 490

To create records into an one2many field, simply pass a list of tuple [(0,0,{'field1':value, 'field2':value, ...})] to the one2many field in the parent class write method ,
eg: self.write( cr, uid, ids, {'one2many_field_name':[ (0, 0, {'field':value}) ] })

Upvotes: 2

Related Questions