通过Wizard Odoo创建新的One2many记录

时间:2015-08-17 07:11:16

标签: python openerp odoo

我想问一下如何通过我的向导创建One2many记录?就像我有一个父类A.在那个表单上我创建了一个One2many字段。放置一个按钮(在父窗体上)以打开向导。该向导有一些选择字段。现在,当我设置选择字段并单击按钮(在向导上)时,我希望在父窗体上的One2many上创建我的过滤记录的所有结果。我不知道如何完成这件事。我的值从向导返回到父表单(简单字段)但我不知道如何在处理One2many字段时完成此操作。下面是我的具有One2many关系类的父类:

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 )

这是我的向导代码:

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})]})

1 个答案:

答案 0 :(得分:2)

要在one2many字段中创建记录,只需传递元组 [(0,0,{'field1':value,'field2':value,...})] 的列表即可父类写入方法中的one2many字段,
 例如:self.write(cr,uid,ids,{'one2many_field_name':[(0,0,{'field':value})]})