user3606682
user3606682

Reputation: 2105

Default Group By two fields tree view Odoo 10

I am trying to make default group by in list view. If I use the below line it is working fine with one field.

<field name="context">{'group_by':'sector_id'}</field>

However my requirement is to group by default with two levels, sector and then nature. So I tried the below code but nothing happens and no error.

Action:

<record id="program_activity_action_window_chairman" model="ir.actions.act_window">
    <field name="name">Activity</field>
    <field name="res_model">program.activity</field>
    <field name="view_ids" eval="[(5,0,0),(0,0,{'view_mode':'tree','view_id':ref('view_program_activity_tree')}),(0,0,{'view_mode':'form','view_id':ref('view_program_activity_form_chairman')})]"/>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form,graph</field>
    <field name="domain">[('state', 'in', ['chairman_approve','done'])]</field>
    <!--<field name="context">{'default_state': 'chairman_approve'}</field>-->
    <!--<field name="context">{'group_by':'sector_id'}</field>-->
    <field name="context">{'search_default_group_sector_id': 1,'search_default_group_nature': 1}</field>

    <field name="help" type="html">
        <p class="oe_view_nocontent_create">
            Create a new Program Activity.
        </p>
    </field>
</record>

Tree View:

<record id="view_program_activity_tree" model="ir.ui.view">
    <field name="name">program.activity.tree</field>
    <field name="model">program.activity</field>
    <field name="arch" type="xml">

        <tree string="Program Activity" colors="green:type_id[1] == 'REVENUE'">
            <field name="department_id"/>
            <field name="sector_id"/>
            <field name="major_program_id"/>
            <field name="minor_program_id"/>
            <field name="name"/>
            <field name="code"/>
            <field name="type_id"/>
            <field name="weight_from_minor"/>
            <field name="total_planned" sum="Total Planned"/>
            <field name="total_actual" sum="Total Actual"/>
            <field name="total_diff_amount" sum="Total Difference Amount"/>
            <field name="total_diff_percentage" sum="Total Difference Percentage %"/>
        </tree>
    </field>
</record>

Please help or any suggestions are most appreciated. Thanks

Upvotes: 2

Views: 6767

Answers (2)

forvas
forvas

Reputation: 10189

Your code seems to be OK, what you must do is to add the nature field to the tree view, and of course both fields must be a filter in the search view of the tree view view_program_activity_tree:

Tree view

<field name="sector_id"/>
<field name="nature" invisible="1"/>

Search view

<filter string="Sector"
    name="group_sector_id"
    domain="[]"
    context="{'group_by':'sector_id'}"
    help="Grouping by sector"/>
<filter string="Nature"
    name="group_nature"
    domain="[]"
    context="{'group_by':'nature'}"
    help="Grouping by nature"/>

In your action there is no search_view_id field, so inherit and modify the default search view of the model program.activity. If it did not exist, create a new search view for it and add the link to it in the action.

<field name="search_view_id" ref="your_search_view_xml_id"/>

Upvotes: 4

CZoellner
CZoellner

Reputation: 14776

If you want a group by without using search view filters (forvas approach) just do a multi group by in context:

<field name="context">{'group_by':['sector_id', 'nature']}</field>

The order in the fields list is important.

Upvotes: 8

Related Questions