Odoo10 层次视图

Odoo10 Hierachy view

我必须在 Odoo10 中为我的模型创建层次结构视图。我做了以下事情。但我能得到它。我希望它看起来像 odoo8 帐户视图图表。

pyhon 文件:

class InconceExpenseDetails(models.Model):
    _name = "income.expense.details"

    parent_id = fields.Many2one("income.expense.details","Income ID")
    child_ids = fields.One2many("income.expense.details","parent_id",string="Income IDS",select=True)
    product_category = fields.Char("Category")
    planned_amount = fields.Float('Planned Amount', digits=0)
    actual_amount = fields.Float('Actual Amount', digits=0)
    variance = fields.Float('Variance', digits=0)
    currency_id = fields.Many2one('res.currency', string="Currency", default=lambda self: self.env.user.company_id.currency_id)
    company_id = fields.Many2one("res.company",string="Company",default=lambda self: self.env.user.company_id)
    type_seq = fields.Char("Sequence", select=1)
    type = fields.Selection([
        ('revenue', 'Revenue'),
        ('income', 'Income'),
        ('expense', 'Expense'),
    ], string="Type")

查看文件:

       <record id="view_budget_tree_view" model="ir.ui.view">
            <field name="name">income.expense.details.tree</field>
            <field name="model">income.expense.details</field>
            <field name="field_parent">child_ids</field>
            <field name="arch" type="xml">
                <tree colors="blue:type == 'income'" string="Income and Expense Details" toolbar="1">
                    <field name="type_seq"/>
                    <field name="type"/>
                    <field name="product_category"/>
                    <field name="planned_amount"/>
                    <field name="actual_amount"/>
                    <field name="variance"/>
                    <field name="currency_id"/>
                    <field name="company_id"/>
                    <field name="parent_id" invisible="1"/>
                </tree>
            </field>
        </record>

行动window:

<record id="action_budget_tree_view" model="ir.actions.act_window">
        <field name="name">income.expense.details.tree</field>
        <field name="res_model">income.expense.details</field>
        <field name="view_type">tree</field>
        <field name="domain">[('parent_id','=',False)]</field>
        <field name="view_id" ref="view_budget_tree_view"/>
    </record>

如果我将其设为普通树视图,则效果很好。但我希望它作为 Odoo8 账户图表中的层次结构视图

您需要计算字段级别:

   level = fields.Integer(compute='_get_level', string='Level', store=True)


   @api.multi
    @api.depends('parent_id', 'parent_id.level')
    def _get_level(self):
        '''Returns a dictionary with key=the ID of a record and value = the level of this  
           record in the tree structure.'''
        for report in self:
            level = 0
            if report.parent_id:
                level = report.parent_id.level + 1
            report.level = level

查看 account 模块中的 account.financial.report

您需要创建一个 Many2many 字段 child_id。 child_ids 已经存在于 One2many 字段中,保持原样。创建另一个字段 child_id(M2m - 计算)

@api.multi
def _get_child_ids(self):
    for record in self:
        result = []
        if record.child_ids:
            result = record.child_ids.ids
        record.child_id = [(6,0,result)]

child_id = fields.Many2many(compute=_get_child_ids, comodel_name="income.expense.details",                               relation='self_rel_child', column1='child_type_id_1', column2='child_type_id_2',string="Income / Expense")

Xml 看起来不错,只需将 One2many (child_ids) 替换为 Many2many (child_id).

   <record id="view_budget_tree_view" model="ir.ui.view">
        <field name="name">income.expense.details.tree</field>
        <field name="model">income.expense.details</field>
        <field name="field_parent">child_id</field>
        <field name="arch" type="xml">
            <tree colors="blue:type == 'income'" string="Income and Expense Details" toolbar="1">
                <field name="type_seq"/>
                <field name="type"/>
                <field name="product_category"/>
                <field name="planned_amount"/>
                <field name="actual_amount"/>
                <field name="variance"/>
                <field name="currency_id"/>
                <field name="company_id"/>
                <field name="parent_id" invisible="1"/>
            </tree>
        </field>
    </record>

动作应该是这样的,

    <record id="action_chart_tree" model="ir.actions.act_window">
        <field name="name">Chart of Income Expense</field>
        <field name="res_model">income.expense.details</field>
        <field name="view_type">tree</field>
        <field name="view_id" ref="view_budget_tree_view"/>
        <field name="domain">[('parent_id','=',False)]</field>
    </record>