This question has been flagged
1 Reply
4777 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!

Avatar
Discard
Best Answer

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 ... 

Avatar
Discard
Author

awesome!! this works! Thanks a lot!

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