我想问一下如何通过向导创建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})]})
最佳答案
要将记录创建到one2many字段中,只需将一个元组[[0,0,{'field1':value,'field2':value,...})]的列表传递给父类write方法中的one2many字段,
例如:self.write(cr,uid,ids,{'one2many_field_name':[(0,0,{'field':value})]})
关于python - 通过Wizard Odoo制作新的One2many记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32044609/