1
votes

I'm trying to create a simple relation in openerp with phyton. I have two tables: one for courses(cursos) and other for teachers (profesores) and I need to create a one2many relation between profesores and cursos, so 1 profesor it teach many cursos and many course can be assigned to a teacher.

I've added a widget="selection" in profesor form, so I can select a course, but when I try to save, appears this error: TypeError: 'int' object is not iterable

This is my code:


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>

Thanks

1

1 Answers

0
votes

I think the problem is the widget selection. Widget selection is used with many2one field only to convert a many2one field to selection box so the user cannot edit it. Therefore using widget selection for one2many field could accidentally return different type of value from what a normal one2many field would return. Normally, one2many field will have special commands with below format for adding/replacing/removing records:

(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)