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

时间:2020-12-15 16:10:35

Is there a way to access child model class object from parent model class object or now what child model class does this parent model class object has?

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

Here are my model classes:

这是我的模型类:

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)

If I have an object of "Content" class say "content1" that has a child object "image1", is there a way to access that "image1" object from "content1" object or now that type of "content1" is "Image"?

如果我有一个“Content”类的对象说“content1”有一个子对象“image1”,有没有办法从“content1”对象访问该“image1”对象,或者现在该类型的“content1”是“Image” “?

Content can have many child classes in future so I don't want to query all the child classes.

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

1 个解决方案

#1


1  

In Odoo you can travel bi-direction but your models should have configured like that,

在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')

And you can access functionality of child classes by calling in this way.

您可以通过这种方式调用来访问子类的功能。

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

Similarly you can call the method of child classes the same way.

类似地,您可以以相同的方式调用子类的方法。

If your aim is to do something else then specify it with example.

如果你的目标是做其他事情,那么用例子来指定它。

#1


1  

In Odoo you can travel bi-direction but your models should have configured like that,

在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')

And you can access functionality of child classes by calling in this way.

您可以通过这种方式调用来访问子类的功能。

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

Similarly you can call the method of child classes the same way.

类似地,您可以以相同的方式调用子类的方法。

If your aim is to do something else then specify it with example.

如果你的目标是做其他事情,那么用例子来指定它。