'int'对象在one2many / many2one关系中不可迭代

时间:2017-03-16 12:20:07

标签: python openerp

我正在尝试用opeton在openerp中创建一个简单的关系。我有两个表:一个用于课程(cursos),另一个用于教师(profesores),我需要在profesores和cursos之间建立一个one2many关系,所以1 profesor它教许多cursos,许多课程可以分配给老师。

我以profesor形式添加了一个widget =“selection”,所以我可以选择一个课程,但是当我尝试保存时,出现这个错误:TypeError:'int'对象不可迭代

这是我的代码:

class profesor(osv.osv):
_name = 'educacion.profesor'
_description = 'Esta clase representa un Profesor'
_columns = {
    'nombre': fields.char('Nombre', size=64, required=True),
    'direccion': fields.char('Direccion', size=200, required=False),
    'telefono': fields.char('Telefono', size=64, required=False),
    'email': fields.char('Email', size=200, required=False),
    'cursos_ids': fields.one2many('educacion.curso','profesor_id','Cursos'),
}

PROFESOR()

class curso(osv.osv):
_name = 'educacion.curso'
_description = 'Esta clase representa un curso'
_columns = {
    'name': fields.char('Curso', size=64, required=True),
    'aula': fields.char('Aula', size=200, required=False),
    'creditos': fields.char('creditos', size=64, required=False),
    'profesor_id': fields.many2one('educacion.profesor', 'Profesores'),
}

CURSO()

<record model="ir.ui.view" id="profesores_form">
        <field name="name">profesores_form</field>
        <field name="model">educacion.profesor</field>
        <field name="type">form</field>
        <field name="arch" type="xml">
            <form string="Profesores">
                <field name="nombre"/>
                <field name="direccion"/>
                <field name="telefono"/>
                <field name="email"/>
                <field name="cursos_ids" widget="selection"/>                   
            </form>
        </field>
    </record>

<record model="ir.ui.view" id="cursos_form">
        <field name="name">cursos_form</field>
        <field name="model">educacion.curso</field>
        <field name="type">form</field>
        <field name="arch" type="xml">
            <form string="Cursos">
                <field name="name"/>
                <field name="aula"/>
                <field name="creditos"/>
            </form>
        </field>
    </record>

由于

1 个答案:

答案 0 :(得分:0)

我认为问题是小工具selection。窗口小部件selection仅与many2one字段一起用于将many2one字段转换为选择框,因此用户无法对其进行编辑。因此,对于one2many字段使用窗口小部件selection可能会意外地从正常的one2many字段返回的不同类型的值返回。通常,one2many字段将具有以下格式的特殊命令,用于添加/替换/删除记录:

(0, 0,  { values })    link to a new record that needs to be created with the given values dictionary
(1, ID, { values })    update the linked record with id = ID (write *values* on it)
(2, ID)                remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
(3, ID)                cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
(4, ID)                link to existing record with id = ID (adds a relationship)
(5)                    unlink all (like using (3,ID) for all linked records)
(6, 0, [IDs])          replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)