pythonodoo

Odoo: one2many and many2one? KeyError:'___'


I am trying to work a bit on building a module in odoo from scratch. I've defined some simple classes but, when trying to define a one2many/many2one field, and install the module, an error occurs:

File "/opt/odoo/src/OCB12/odoo/fields.py", line 2492, in _setup_regular_full invf = comodel._fields[self.inverse_name] KeyError: 'autor_id'

If I comment the line where the problem seems to happen, it won't complain during installation. BUT the field seems ok to me; i ve checked the guides and cannot find the error. I suspect my problem can be somewhere else in the Odoo module (maybe it is not the case)

class helados_receta(models.Model):
    _name = 'helados.receta'
    _order = 'sabor desc, name'

    sabor = fields.Char('Sabor', required=True)
    data_alta = fields.Date('Data de alta')
    state = fields.Selection(
        [('desarrollo', 'Desarrollo'),
         ('experimental', 'Experimental'),
         ('producción', 'En producción')],
        'State', default="desarrollo")
    temperatura_celsius=fields.Float("Temperatura Conservación", required=True)
    receta = fields.Text("Receta", required=True)
    autor_id = fields.Many2one('helados.repostero', string='Autor')

    class helados_repostero(models.Model):
        _name = 'helados.repostero'
        _inherit = 'res.partner'
    
        helados_ids=fields.One2many('helados.receta', 'autor_id', string='Recetas')
        name=fields.Char("Nombre y apellidos", required=True)
        partner_id = fields.Many2one('res.partner', ondelete='cascade')
        local = fields.Char("Lugar de Trabajo", required=True)

You can find the whole module here: https://github.com/conversorbinario/heladeria_odoo Thanks


Solution

  • You need to define the inverse field name autor_id in helados.receta model of type Many2one that references helados.repostero model.

     autor_id = fields.Many2one('helados.repostero')
    

    You have redefined the helados.receta model in helado.py file, so if you define the inverse field before you will get the same error. I think you forgot to use _inherit instead of _name.