Search code examples
xmlodoo-8searchviewodooodoo-10

How to make a search view between 2 dates?


I got a model (Activity) with a Field Date ("date") on it. And I want to implement on my module a search view than finds the activities between 2 dates.

Anyone knows how? I made a search view by description but dont know how to implement this:

<record model="ir.ui.view" id="activity_search_view">
    <field name="name">activity.search</field>
    <field name="model">proyectosge.activity</field>
    <field name="arch" type="xml">
        <search>
            <field name="description"/>
        </search>
    </field>
</record>

Solution

  • Static and relative dates

    If the dates are always the same you can create a filter like this

    <filter string="This Year" 
            name="thisyear"
            domain="['|', ('date', '=', False), '&amp;',('date','&lt;=', time.strftime('%%Y-12-31')),('date','&gt;=',time.strftime('%%Y-01-01'))]" 
            help="Journal invoices with period in current year" />
    

    So, take a look at the domain, more examples (this year, this month, today, static dates):

    domain="[('date','&lt;=', time.strftime('%%Y-12-31')),('date','&gt;=',time.strftime('%%Y-01-01'))]"
    domain="[('date','&lt;=', time.strftime('%Y-%m-%d')),('date','&gt;=',time.strftime('%Y-%m-01'))]"
    domain="[('date','&lt;=', datetime.datetime.combine(context_today(), datetime.time(23,59,59))), ('date','&gt;=', datetime.datetime.combine(context_today(), datetime.time(0,0,0)))]"
    domain="[('date','&lt;=', time.strftime('2020-12-31')),('date','&gt;=',time.strftime('2000-01-01'))]"
    

    Dynamic dates

    If they are not static you must add both conditions with the advanced search. You can add the filter to favourites as well

    enter image description here

    Note: Keep in mind that you have to press the "Apply" to add each condition in the advanced search to use an "and" operator. Don´t press the button "Add a condition" because you are going to use an "or" operator