Date Filters

Start here if you want to understand Filters

Odoo has various date filters.

Standard Filters

Very simple to implement:

<filter string="Order Date" name="order_date" date="date_order">

[This is a filter in a Search View, more explanation here and below]

There’s no need to specify that you want to see the last three months, or four quarters or the last three years, that’s all done automatically.

Customized filters

Sometimes you want something a bit different, e.g. orders in the last 4 weeks (or any period of time):

This is a bit more complex. Here’s the Extension Search View:

<?xml version="1.0"?>

<xpath expr="//filter[@name='my_sale_orders_filter']" position="after">

  <filter string="Last 4 weeks" name="four_weeks"

domain="[('date_order','&gt;=',(context_today() + relativedelta(weeks=-4, weekday=0)).strftime('%Y-%m-%d'))]"

        />

</xpath>

That might look a bit scary, but all you really need to understand are:

  1. domains
  2. the order date field is date_order (you can select another date field)
  3. the parameters for relativedelta
    • In this case we want the previous 4 weeks so the parameter is weeks=-4
    • By the same logic, the next two weeks would be weeks=2
      • You’d probably also need to change &gt;= to &lt;=

Here’s another example:

<filter string="Expire This Week" name="expire_this_week"

domain="['&amp;',('expiry_date', '&gt;=', (context_today() + relativedelta(weeks=-1,days=1,weekday=0)).strftime('%Y-%m-%d')),('expiry_date', '&lt;=', (context_today() + relativedelta(weekday=6)).strftime('%Y-%m-%d'))]"/>

This one has a date range – from the beginning of this week to the end of the week. Note that Monday is weekday 0 and Sunday is weekday 6.

Using date filters to select customers

This one is based on answer on the Odoo Help Forum from Paresh Wagh. The requirement is to select customers who have confirmed sales orders in the current calendar year.

This uses a new field and an Extension View

New field

  • Model: Contact [res.partner]
  • Field Label: Sales Order dates
  • Field name: x_sales_order_dates
  • Type: datetime
  • Related field: sale_order_ids.date_order

Extension View

<?xml version="1.0"?>

<xpath expr="//filter[@name='inactive']" position="before">

      <filter string="Orders this year" name="orders"

        domain="[('x_sales_order_dates','&lt;=', time.strftime('%%Y-12-31')),

                 ('x_sales_order_dates','&gt;=',time.strftime('%%Y-01-01')),

                 ('sale_order_ids.state', 'not in', ('draft', 'sent', 'cancel'))]"/>

    <separator/>

</xpath>

To select orders in the last 12 months, we can create another filter with a different domain

<filter string="Orders last 12 months" name="orders_12m"
    domain="[('x_sales_order_dates','&gt;=',(context_today() + relativedelta(months=-12, weekday=0)).strftime('%Y-%m-%d')),
('sale_order_ids.state', 'not in', ('draft', 'sent', 'cancel'))]"/>