如何在Odoo 10中使用向导?

时间:2018-06-21 06:50:12

标签: python xml odoo wizard

这是我的代码:

    class StudentWizard(models.TransientModel):
        _name = 'student.wizard'

        def _get_default_students(self):
            return self.env['ecole.partner.school'].browse(self.env.context.get('active_ids'))

        student_ids = fields.Many2many('ecole.partner.school', String="Student", default=_get_default_students)
        school_name = fields.Many2one(comodel_name="horanet.school.establishment",
                                      string="Etablissement Scolaire",
                                      required=True)
        school_year = fields.Many2one(string='Période',
                                      ondelete='SET NULL',
                                      comodel_name="ecole.partner.school.years",
                                      required=True)
        school_level = fields.Many2one(comodel_name="horanet.school.grade",
                                       string="Niveau Scolaire",
                                       required=True)
        #level = fields.Char('Level')

        api.multi
        def set_student_level(self):
            for record in self:
                if record.student_ids:
                    for student in record.student_ids:
                        update_student = self.env['ecole.partner.school']
                        partner_id = update_student.partner_id
                        school_year = update_student.school_year
                        school_name = update_student.school_name
                        half_pension = update_student.half_pension
                        nursery = update_student.nursery

                        student.school_year = school_year
                        student.school_level = self.school_level
                        student.school_name = school_name

                        vals = {'partner_id': partner_id, 'nursery': nursery}
                        res = update_student.create(vals)

                    return res

My View:

        <act_window id="student_wizard_action" name="Mise à jour des élèves" res_model="student.wizard"
                    src_model="ecole.partner.school"
                    view_mode="form" target="new" multi="True"/>

        <record id="student_wizard_view" model="ir.ui.view">
            <field name="name">student Marks Views</field>
            <field name="model">student.wizard</field>
            <field name="arch" type="xml">
                <form>
                    <group>
                        <field name="student_ids" widget="many2many_list"/>
                        <field name="school_name"/>
                        <field name="school_year"/>
                        <field name="school_level"/>
                    </group>
                    <footer>
                        <button name="set_student_level" string="Passage au niveau suivant" class="oe_right oe_highlight"
                                type="object"/>
                        <button string="Cancel" class="oe_right" special="cancel"/>
                    </footer>
                </form>
            </field>
        </record>

我想在我的模型school.partner.school中创建一条新记录,问题是我创建的模型是空的。我不明白为什么。

1 个答案:

答案 0 :(得分:0)

要更新记录,您必须先找到它!

模型'ecole.partner.school'不能是瞬态模型。

请创建适合您要求的域,而不是[]。

@api.multi
def set_student_level(self):
    domain = []

    for record in self:
        for student in record.student_ids:
            update_student = self.env['ecole.partner.school'].search(domain,limit=1)
            if update_student:
                partner_id = update_student.partner_id
                school_year = update_student.school_year
                school_name = update_student.school_name
                half_pension = update_student.half_pension
                nursery = update_student.nursery
                student.school_year = school_year
                student.school_level = self.school_level
                student.school_name = school_name
                vals = {'partner_id': partner_id, 'nursery': nursery}
                res = update_student.create(vals)
    return True