Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
357 Vistas

Hi everyone,

 
I'm working on two add-ons. In one add-on, I define an abstract model to use as a base (A), and I also define two concrete models that inherit from this base model (B and C)

In another add-on, I use the base model as a relation in another model (D). My idea was that this would display the items from the concrete models. However, I get an error when I try to create model D


class A(models.AbstractModel):
_name = "a"

class B(models.Model):
_name = "b"
_inherit = "a"

class C(models.Model):
_name = "c"
_inherit = "a"

class D(models.Model):
a_id = fields.Many2one("a", string="A")

The error: 

  File "/odoo/odoo/models.py", line 4045, in _read_format
    vals[name] = convert(record[name], record, use_display_name)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/odoo/odoo/fields.py", line 3295, in convert_to_read
    return value.id
           ^^^^^^^^
AttributeError: 'A' object has no attribute 'id'

I tried changing model A to a concrete model, and that made the error disappear. However, it doesn't show the children B and C when I try to create model D. I also tried instantiating model A when creating models B and C. In this case, it appears when I create model D, but I lose the ability to search and filter by features of models B and C.

Is this possible, or am I wrong in trying to make this relationship with the abstract model? 

Any pointers appreciated!

Avatar
Descartar
Mejor respuesta

The issue you're encountering is because abstract models in Odoo do not create database tables, meaning they don’t have an id field that can be used in foreign key relationships like a Many2one. In your case, when you try to use Many2one("a") in model D, it fails because model A is abstract and doesn't exist as a physical table with an id.

Avatar
Descartar
Mejor respuesta

Odoo doesn't work that way. An abstract model means there is no table in postgres database created and thus will not have any id or foreign key. If you want foreign key to it, make it a normal model. It may make your database a little more complicated and less optimized but with out your model, it is already so. One more table wouldn't hurt much.


Avatar
Descartar