OpenERP Search Filter: Last Three Months

The key to building this kind of filters is relativedelta , please read its manual for more details.

Here is an example:

1:  <!-- filter: last three months -->
2:  <filter icon="terp-personal" name="last_three_month"
3:          string="Last 3 Months"
4:          domain="[('date','&lt;=',time.strftime('%%d/%%m/%%Y')),
5:                   ('date','&gt;=',
6:                    ((context_today() -
7:                     relativedelta(months=3)).strftime('%%d/%%m/%%Y')))
8:                  ]"/>

Explanation:

  • Line #4: time.strftime('%%d/%%m/%%Y') returns the current date; &lt;= is the less-than sign in XML. This line means: “before today”.
  • Line #5,6,7: context_today() is another way to get the current date in OpenERP, subtract it with relativedelta(months=3) to get the date three months ago. So these three lines mean: “greater than three months ago”, &gt;= is the greater-than sign in XML.
  • All together: “from three months ago to today”, aka, “last three months”

Note:

  • This link helped a lot
  • Thanks to mrshelly
  • Thanks to my colleague Tom

Happy Hacking!


Date: 2013-08-07

Author: 任文山 (Ren Wenshan)

Org version 7.9.3d with Emacs version 24

Validate XHTML 1.0

One thought on “OpenERP Search Filter: Last Three Months