如何使用odoo 10中的瞬态模型(向导)将数据插入表中

时间:2017-03-07 12:54:25

标签: openerp odoo-10

我有一个名为Vehicle的模型,我创建了一个向导od_add_new_vehicle。 在向导的xml视图中,我创建了一个动作。 如何将Vehicle模型与向导链接,以便我可以永久保存数据

class Vehicle(models.Model):
    _name = 'transport.vehicle'

    name = fields.Char(string="Name", required=True)
    description = fields.Text()
    reg_date = fields.Date()
    department = fields.Char()
    available = fields.Boolean()


class od_add_new_vehicle(models.TransientModel):
    _name = 'od.add.new.vehicle'
    _description = 'Add new vehicle'
    name = fields.Char('vehicle name')
    description = fields.Text('Description')
    reg_date = fields.Date('Reg date')
    department = fields.Char('Department')
    available = fields.Boolean('Available')

    def _default_veh(self):
        return self.env['transport.vehicle'].browse(self._context.get('active_ids'))

    v_id = fields.Many2one('transport.vehicle', string="Vehicle ref", required=True, default=_default_veh)

    @api.multi
    def od_add_book(self):
        self.v_id.name = self.name
        self.v_id.description = self.description
        self.v_id.reg_date = self.reg_date
        self.v_id.department = self.department
        self.v_id.available = self.available
        self.v_id.member_id = self.member_id
        return {}

1 个答案:

答案 0 :(得分:2)

以下是从向导更新/插入数据的简单方法。

  • 在表单视图页脚上添加按钮
  • 按钮类型将是 object 以在.py端调用python函数
  • 使用 active_ids 更新记录集。

例如:

<footer>
    <button name="insert_data" string="Insert/Update data" type="object"
        class="oe_highlight"/>
    or
    <button string="Cancel" class="oe_link" special="cancel" />
</footer>

.py边功能

@api.multi
def insert_data(self):

    for record in self.env['target.table.name'].browse(self._context.get('active_ids', [])):

        #here you can access target table fields using *record* variable

        record.field_name = wizard.field_name

    return True