Openerp 动态显示多个one2many字段(具有不同的域)?

Openerp 动态显示多个one2many字段(具有不同的域)?,openerp,odoo-8,Openerp,Odoo 8,我有一个模型(modelA),其中有一个2many字段与另一个模型(modelB)相关,modelB中的一个字段是category字段,这是一个manyOne字段。要求为每个类别显示一个one2many字段。因此,如果有两个名为“category1”和“category2”的类别,modelA的表单视图应该有两个one2many字段,一个显示具有category1的记录,另一个显示category2的记录(可能使用域完成) 例如,modelA和modelB具有以下结构 A类(models.Mod

我有一个模型(modelA),其中有一个2many字段与另一个模型(modelB)相关,modelB中的一个字段是category字段,这是一个manyOne字段。要求为每个类别显示一个one2many字段。因此,如果有两个名为“category1”和“category2”的类别,modelA的表单视图应该有两个one2many字段,一个显示具有category1的记录,另一个显示category2的记录(可能使用域完成)

例如,modelA和modelB具有以下结构

A类(models.Model):
_名称='modelA'
modelA_one2manyfield=fields.One2many('modelB','modelB_many2onefield'))
B类(models.Model):
_名称='modelB'
name=fields.Char()
类别=字段。manyOne('modelC')
modelB_manyOne字段=fields.manyOne('modelA')

如何为modelA实现表单视图,以便每个类别(用户可以添加这些类别,因此可以有任意数量的类别)都可以这里有一个单独的one2many字段。

您需要花费大量时间才能给出一个非常好的答案。我认为您需要尝试的方法之一是覆盖
字段\u view\u get
,因为这是检索视图的方法,在这里您可以更改
arch
字段以添加一个costum字段。请看一下教程:

但我认为您会遇到一个问题,因为即使将域放在XML中的one2many字段上,odoo也不会过滤 加载发生在视图上时的记录:


但是当我将这个字段添加到python声明中时

#在这里,视图上不会显示任何记录,这是预期的结果
one2many_field_name=fields.one2many(…,domain=[('id','=',False)])

因此,通过
字段\u视图\u get
向arch添加一个2many字段的问题很简单,但问题是过滤数据

您所问的问题需要花费大量时间才能给出一个非常好的答案,我认为您需要尝试的方法之一是覆盖
字段\u view\u get
,因为这是检索视图的方法,在这里您可以更改
arch
字段以添加肋部字段。请参阅本教程:

但我认为您会遇到一个问题,因为即使将域放在XML中的one2many字段上,odoo也不会过滤 加载发生在视图上时的记录:


但是当我将这个字段添加到python声明中时

#在这里,视图上不会显示任何记录,这是预期的结果
one2many_field_name=fields.one2many(…,domain=[('id','=',False)])

因此,通过
字段\u视图\u get
向arch添加一个2many字段的问题很简单,但问题是过滤数据

这在技术上是不可能的。因为在同一视图中,同一字段不能有2倍

但是您可以创建一个特定的小部件来显示您想要的内容。如何在时间表视图(我的当前时间表菜单)中查看

这是一个创建小部件的小教程。
这在技术上是不可能的。因为在同一视图中,同一字段不能有2倍

但是您可以创建一个特定的小部件来显示您想要的内容。如何在时间表视图(我的当前时间表菜单)中查看

这是一个创建小部件的小教程。

这不是答案,但您可以说动态视图的教程示例: 模块结构:

->dynamic_view
    --> __ini__.py
    --> models.py
    --> views.xml
    --> __manifest__.py
\uuuu清单\uuuuu.py

# -*- coding: utf-8 -*-
        {
            'name' : 'Dynamic view',
            'version' : '1.0',
            'summary': 'Tutorial for Dynamic view',
            'sequence': 30,
            'description': """
             This Module is for showing that you can update the code of the view
             when it's called and even create new field without having to use python
             code at all
            """,
            'category': 'StackOverFlow',
            'depends' : ['base_setup',],
            'data': [
                'views.xml'
            ],
            'installable': True,
            'application': True,
            'auto_install': False,
        }
        # -*- coding: utf-8 -*-
        from . import models
# -*- coding: utf-8 -*-

        from odoo import models, fields, api


        class Person(models.Model):
            _name = "training.person"

            name = fields.Char("Full name")


        class Car(models.Model):
            _name = "training.car"
            name = fields.Char("Car name")
            mark_id = fields.Many2one(comodel_name="training.mark", string="Mark")
            owner_id = fields.Many2one(comodel_name="training.person", string="Owner")


        person_view_id = "dynamic_view.dgapr_form_person"
        # here default arch value body in the view contains only
        # name field but as we create new mark we add others field
        person_view_arch = """
                <group>
                    <field name="name"/>
                </group>
        """


        class Mark(models.Model):

            _name = "training.mark"

            name = fields.Char("Mark")

            @api.model
            def create(self, values):
                """
                   when we create a category we add one2many field to person view
                   TODO: when we unlink a category we need to remove the one2many
                         name of field is : x_mark_{id of deleted record}
                """
                rec_id = super(Mark, self).create(values)
                o2m_field = {
                    # fields created using orm method must start with x_
                   "name": "x_mark_%s"% rec_id.id,
                   "field_description": "Mark %s" % rec_id.name,
                   "ttype": "one2many",
                   "relation": "training.car",
                   "relation_field": "owner_id",
                   "stored": True,
                   "domain": "[('mark_id','=', %s)]"%rec_id.id,
                   "model_id": self.env.ref("dynamic_view.model_training_person").id,
                }
                # add on2many field to ir.model.fields
                self.env["ir.model.fields"].create(o2m_field)
                self.update_arch()
                return rec_id

            def update_arch(self):
                """
                when ever we create or delete a mark record
                we need to update the the view to add new one2many field
                if we want to hide the one2many field in view that don't have
                any record we should create compute field to use attrs features
                """
                view_id = self.env.ref(person_view_id)
                o2m_fields_ids = self.env['ir.model.fields'].search(
                    [
                        ('model_id', '=', self.env.ref("dynamic_view.model_training_person").id),
                        ('ttype', 'like', 'one2many'),
                        ('relation_field', 'like', 'owner_id')
                     ])
                o2many_arch = ""
                for o2m_id in o2m_fields_ids:
                    o2many_arch = o2many_arch + """

                        <group col="1" string="%s">
                            <field name="%s" noloable="1" />
                        </group>

                        """ % (o2m_id.field_description, o2m_id.name,)

                arch_begin = """
                     <form>
                        <sheet>
                    """
                arch_close = """
                        </sheet>
                     </form>
                     """
                arch_body = person_view_arch + o2many_arch
                new_arch = arch_begin + arch_body + arch_close

                # update the arch of the view in database
                view_id.arch = new_arch
\uuuu init\uuuuu.py

# -*- coding: utf-8 -*-
        {
            'name' : 'Dynamic view',
            'version' : '1.0',
            'summary': 'Tutorial for Dynamic view',
            'sequence': 30,
            'description': """
             This Module is for showing that you can update the code of the view
             when it's called and even create new field without having to use python
             code at all
            """,
            'category': 'StackOverFlow',
            'depends' : ['base_setup',],
            'data': [
                'views.xml'
            ],
            'installable': True,
            'application': True,
            'auto_install': False,
        }
        # -*- coding: utf-8 -*-
        from . import models
# -*- coding: utf-8 -*-

        from odoo import models, fields, api


        class Person(models.Model):
            _name = "training.person"

            name = fields.Char("Full name")


        class Car(models.Model):
            _name = "training.car"
            name = fields.Char("Car name")
            mark_id = fields.Many2one(comodel_name="training.mark", string="Mark")
            owner_id = fields.Many2one(comodel_name="training.person", string="Owner")


        person_view_id = "dynamic_view.dgapr_form_person"
        # here default arch value body in the view contains only
        # name field but as we create new mark we add others field
        person_view_arch = """
                <group>
                    <field name="name"/>
                </group>
        """


        class Mark(models.Model):

            _name = "training.mark"

            name = fields.Char("Mark")

            @api.model
            def create(self, values):
                """
                   when we create a category we add one2many field to person view
                   TODO: when we unlink a category we need to remove the one2many
                         name of field is : x_mark_{id of deleted record}
                """
                rec_id = super(Mark, self).create(values)
                o2m_field = {
                    # fields created using orm method must start with x_
                   "name": "x_mark_%s"% rec_id.id,
                   "field_description": "Mark %s" % rec_id.name,
                   "ttype": "one2many",
                   "relation": "training.car",
                   "relation_field": "owner_id",
                   "stored": True,
                   "domain": "[('mark_id','=', %s)]"%rec_id.id,
                   "model_id": self.env.ref("dynamic_view.model_training_person").id,
                }
                # add on2many field to ir.model.fields
                self.env["ir.model.fields"].create(o2m_field)
                self.update_arch()
                return rec_id

            def update_arch(self):
                """
                when ever we create or delete a mark record
                we need to update the the view to add new one2many field
                if we want to hide the one2many field in view that don't have
                any record we should create compute field to use attrs features
                """
                view_id = self.env.ref(person_view_id)
                o2m_fields_ids = self.env['ir.model.fields'].search(
                    [
                        ('model_id', '=', self.env.ref("dynamic_view.model_training_person").id),
                        ('ttype', 'like', 'one2many'),
                        ('relation_field', 'like', 'owner_id')
                     ])
                o2many_arch = ""
                for o2m_id in o2m_fields_ids:
                    o2many_arch = o2many_arch + """

                        <group col="1" string="%s">
                            <field name="%s" noloable="1" />
                        </group>

                        """ % (o2m_id.field_description, o2m_id.name,)

                arch_begin = """
                     <form>
                        <sheet>
                    """
                arch_close = """
                        </sheet>
                     </form>
                     """
                arch_body = person_view_arch + o2many_arch
                new_arch = arch_begin + arch_body + arch_close

                # update the arch of the view in database
                view_id.arch = new_arch
models.py

# -*- coding: utf-8 -*-
        {
            'name' : 'Dynamic view',
            'version' : '1.0',
            'summary': 'Tutorial for Dynamic view',
            'sequence': 30,
            'description': """
             This Module is for showing that you can update the code of the view
             when it's called and even create new field without having to use python
             code at all
            """,
            'category': 'StackOverFlow',
            'depends' : ['base_setup',],
            'data': [
                'views.xml'
            ],
            'installable': True,
            'application': True,
            'auto_install': False,
        }
        # -*- coding: utf-8 -*-
        from . import models
# -*- coding: utf-8 -*-

        from odoo import models, fields, api


        class Person(models.Model):
            _name = "training.person"

            name = fields.Char("Full name")


        class Car(models.Model):
            _name = "training.car"
            name = fields.Char("Car name")
            mark_id = fields.Many2one(comodel_name="training.mark", string="Mark")
            owner_id = fields.Many2one(comodel_name="training.person", string="Owner")


        person_view_id = "dynamic_view.dgapr_form_person"
        # here default arch value body in the view contains only
        # name field but as we create new mark we add others field
        person_view_arch = """
                <group>
                    <field name="name"/>
                </group>
        """


        class Mark(models.Model):

            _name = "training.mark"

            name = fields.Char("Mark")

            @api.model
            def create(self, values):
                """
                   when we create a category we add one2many field to person view
                   TODO: when we unlink a category we need to remove the one2many
                         name of field is : x_mark_{id of deleted record}
                """
                rec_id = super(Mark, self).create(values)
                o2m_field = {
                    # fields created using orm method must start with x_
                   "name": "x_mark_%s"% rec_id.id,
                   "field_description": "Mark %s" % rec_id.name,
                   "ttype": "one2many",
                   "relation": "training.car",
                   "relation_field": "owner_id",
                   "stored": True,
                   "domain": "[('mark_id','=', %s)]"%rec_id.id,
                   "model_id": self.env.ref("dynamic_view.model_training_person").id,
                }
                # add on2many field to ir.model.fields
                self.env["ir.model.fields"].create(o2m_field)
                self.update_arch()
                return rec_id

            def update_arch(self):
                """
                when ever we create or delete a mark record
                we need to update the the view to add new one2many field
                if we want to hide the one2many field in view that don't have
                any record we should create compute field to use attrs features
                """
                view_id = self.env.ref(person_view_id)
                o2m_fields_ids = self.env['ir.model.fields'].search(
                    [
                        ('model_id', '=', self.env.ref("dynamic_view.model_training_person").id),
                        ('ttype', 'like', 'one2many'),
                        ('relation_field', 'like', 'owner_id')
                     ])
                o2many_arch = ""
                for o2m_id in o2m_fields_ids:
                    o2many_arch = o2many_arch + """

                        <group col="1" string="%s">
                            <field name="%s" noloable="1" />
                        </group>

                        """ % (o2m_id.field_description, o2m_id.name,)

                arch_begin = """
                     <form>
                        <sheet>
                    """
                arch_close = """
                        </sheet>
                     </form>
                     """
                arch_body = person_view_arch + o2many_arch
                new_arch = arch_begin + arch_body + arch_close

                # update the arch of the view in database
                view_id.arch = new_arch
#-*-编码:utf-8-*-
来自odoo导入模型、字段、api
班级负责人(models.Model):
_name=“training.person”
name=fields.Char(“全名”)
等级车(型号.型号):
_name=“training.car”
name=fields.Char(“汽车名称”)
mark\u id=fields.manyOne(comodel\u name=“training.mark”,string=“mark”)
所有者id=fields.manyOne(comodel_name=“training.person”,string=“所有者”)
person\u view\u id=“dynamic\u view.dgapr\u form\u person”
#此处,视图中的默认拱门值主体仅包含
#名称字段,但当我们创建新标记时,我们会添加其他字段
“个人视图”(person_view_arch=“”)
"""
课程分数(models.Model):
_name=“training.mark”
name=fields.Char(“标记”)
@api.model
def创建(自身、值):
"""
当我们创建一个类别时,我们将one2many字段添加到person视图中
TODO:当我们取消一个类别的链接时,我们需要删除其中的一个
字段名称为:x_标记{已删除记录的id}
"""
rec_id=super(标记,自我)。创建(值)
o2m_字段={
#使用orm方法创建的字段必须以x开头_
“名称”:“x标记%s”%rec\u id.id,
“字段描述”:“标记%s”%rec\u id.name,
“ttype”:“one2many”,
“关系”:“培训.汽车”,
“关系字段”:“所有者id”,
“存储”:正确,
“域”:“[('mark_id','=',%s)]%rec_id.id,
“模型id”:self.env.ref(“动态视图.模型培训人员”).id,
}
#将2个任意字段添加到ir.model.fields
self.env[“ir.model.fields”].create(o2m_字段)
self.update_arch()
返回记录id
def更新(自我):
"""
当我们创建或删除标记记录时
我们需要更新视图以添加新的one2many字段
如果我们想隐藏视图中的one2many字段,但该字段没有
我们应该创建计算字段以使用ATTR功能的任何记录