Tuesday, 3 June 2014

Add Date range in Group by search filters in Openerp 7 ( Odoo )

Example -  You want to add this filter in tasks.
       In project.task object there are 2 fields - date_start and date_end , and we can use these fields for our date range filter

To add date from and date to filter in openerp , you can follow these steps:

1. import these packages in py file

import netsvc
from osv import fields, osv
from tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT, DATETIME_FORMATS_MAP, float_compare

2. inherit project_task class and add two fields - task_date_from and task_date_to, to be used for filter -
    class project_task(osv.osv):
        _inherit = "project.task"

        _columns = {
            'task_date_from':fields.function(lambda *a,**k:{}, method=True, type='date',string="Task date from"),
            'task_date_to':fields.function(lambda *a,**k:{}, method=True, type='date',string="Task date to"),

        }

3. Now add task_date_from and task_date_to fields in the view -
   For more interactive search view , you can use widget="calendar"

    <record id="view_task_search_form_inherit" model="ir.ui.view">
            <field name="name">project.task.search</field>
            <field name="model">project.task</field>
            <field name="inherit_id" ref="project.view_task_search_form" />
            <field name="arch" type="xml">
                <xpath expr="/search[@string='Tasks']/field[@name='project_id]" position="after">
                    <field name="task_date_from" filter_domain="[('date_start','&gt;=',self)]" widget="calendar"/>
                    <field name="task_date_to" filter_domain="[('date_end','&lt;=',self)]" widget="calendar"/>
                </xpath>
                
            </field>
        </record>



Thanks 
Sonu Chaudhary 

No comments:

Post a Comment