Openerp 从Odoo8中的父模型类对象访问子模型类

Openerp 从Odoo8中的父模型类对象访问子模型类,openerp,odoo,openerp-8,odoo-8,Openerp,Odoo,Openerp 8,Odoo 8,有没有办法从父模型类对象访问子模型类对象,或者现在该父模型类对象有哪个子模型类 以下是我的模型课程: class Content(models.Model): _name = 'content' title = fields.Char(string='Title', required=False) summary = fields.Char(string='Summary', required=False) description = fields.Char(s

有没有办法从父模型类对象访问子模型类对象,或者现在该父模型类对象有哪个子模型类

以下是我的模型课程:

class Content(models.Model):
    _name = 'content'

    title = fields.Char(string='Title', required=False)
    summary = fields.Char(string='Summary', required=False)
    description = fields.Char(string='Description', required=False)


class Video(models.Model):
    _name = 'video'
    _inherits = {'content': 'content_id'}

    duration = fields.Float(string='Duration', required=False)


class Image(models.Model):
    _name = 'image'
    _inherits = {'content': 'content_id'}

    width = fields.Float(string='Width', required=False)
    height = fields.Float(string='Height', required=False)
如果我有一个“Content”类的对象,比如说“content1”,它有一个子对象“image1”,那么有没有办法从“content1”对象访问“image1”对象,或者现在“content1”的类型是“Image”


内容将来可能会有很多子类,所以我不想查询所有子类。

在Odoo中,您可以双向移动,但您的模型应该配置为这样

class Content(models.Model):
    _name = 'content'
    _rec_name='title'

    title = fields.Char(string='Title', required=False)
    summary = fields.Char(string='Summary', required=False)
    description = fields.Char(string='Description', required=False)
    video_ids : fields.One2many('video','content_id','Video')
    image_ids : fields.One2many('image','content_id','Video')

class Video(models.Model):
    _name = 'video'
    _inherit = 'content'

    duration = fields.Float(string='Duration', required=False)
    content_id = fields.Many2one('content','Content')

class Image(models.Model):
    _name = 'image'
    _inherit = 'content'

    width = fields.Float(string='Width', required=False)
    height = fields.Float(string='Height', required=False)
    content_id = fields.Many2one('content','Content')
您可以通过这种方式调用来访问子类的功能

for video in content1.video_ids:
    ## you can access properties of child classes like... video.duration

for image in content1.image_ids:
    print image.width
类似地,您可以用同样的方法调用子类的方法


如果您的目标是做其他事情,请用示例说明。

实际上,我要避免的是每次创建新的子类时都修改父类,即在父类中添加“_id=fields.One2many()”字段。如果我不能从父类的对象访问子类的对象,这是可以的,只知道父类对象属于哪个子类就足够了,这样我就不必查询所有子类,而只需查询一个子类。所以我在寻找一种解决方案,比如在父类中添加一个类似“child_table”的字段,并且每个子类都用它的“\u name”属性填充这个父字段。我认为这样做会容易得多。以适当的方式维持关系总是明智的,这样你就可以得到oops允许的所有自由。