Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Search in year ranges

By
Diego Calzadilla
on 3/26/15, 2:04 PM 592 views

Hi all

I have defined these fields:

   'date_beg': fields.selection([(num, str(num)) for num in range(1900, (datetime.now().year)+1 )], 'Año inicial'),

   'date_end': fields.selection([(num, str(num)) for num in range(1900, (datetime.now().year)+1 )], 'Año final'),

They generate integer variables in my table:

ID          date_beg       date_end

1              1999                2005

2              2000                2004

3              1990                1995


My requirement is that when user types a year in search view, odoo will show the records on which this year belongs based on ranges.

For example, If I type '2003' in search view and I have above table values, then odoo will display records with id value 1 & 2.

Please, some suggestions!!

Thanks in advance!

1

Jérémy Kersten (jke)

--Jérémy Kersten (jke)--
2993
| 3 2 5
Jodoigne, Belgium
--Jérémy Kersten (jke)--

Jérémy is a member of the core R&D team since september 2013. He developed several projects for OpenERP version 8 including the eCommerce, the Google Calendar synchronization and the new product variants/configurator. Now, he manages Odoo website and themes for saas.

Jérémy Kersten (jke)
On 3/26/15, 6:01 PM

Hello,

A way, (maybe not the best) is to add a function field with a function search to adapt the query.

And you can Add a filter "By year" in search view to make it invisible for end user...


def _search_year(self, cr, uid, obj, name, args, context):
x = [('date_beg', '>=', args[0][2]), ('date_end', '<=', args[0][2])]
res = self.search(cr, uid, x, context=context)
return [('id', 'in', res)]

...

    'date_beg': fields.selection([(num, str(num)) for num in range(1900, (datetime.now().year)+1 )], 'Año inicial'),
'date_end': fields.selection([(num, str(num)) for num in range(1900, (datetime.now().year)+1 )], 'Año final'),
'date_search': fields.function(lambda self: self, string='Año search', type='integer', fnct_search=_search_year)

Let us know it it works ... 

awesome!! this works! Thanks a lot!

MEXYTUL, Diego Calzadilla
on 3/27/15, 2:31 AM

It is a sample but you need to check all args and not only take the first one ... [0][2] should disappear ...

Jérémy Kersten (jke)
on 3/27/15, 3:16 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)

Stats

Asked: 3/26/15, 2:04 PM
Seen: 592 times
Last updated: 11/4/15, 8:06 PM