2017-07-25 86 views
1

我必須在Odoo10中爲我的模型創建一個層次結構視圖。我做了以下。但我可以得到它。我希望這看起來像odoo8賬戶視圖的圖表。Odoo10層次視圖

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> 

法案窗口:

<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

回答

1

您需要創建一個Many2many字段child_id。 one2many字段中的child_ids已經存在,請保持原樣。創建另一個場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> 
1

的會計科目,你需要計算領域級別:

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