Skip to Content
Menu
This question has been flagged
3 Replies
10777 Views

I used this code for the data in the selection


    def _get_years(self):

        this_year = datetime.today().year


        results = [(str(x), str(x)) for x in range(this_year - 40, this_year + 1)]

        return results


year_selection = fields.Selection(_get_years, string="Select Year", default=current_year)

Avatar
Discard
Best Answer

Just write following code and call this method into selection field like :

  def _get_years(self):

        this_year = datetime.today().year

        results = sorted([(str(x), str(x)) for x in range(this_year - 40, this_year + 1)],reverse = True)

        return results

year_selection : fields.Selection(_get_years,string="select year)

Avatar
Discard
Author

Thank you! It worked.

Best Answer

Hi Dee Yoj,

Try This Code 

from datetime import datetime
from odoo import fields, models, api


class YourClass(models.Model):
_inherit = 'your.model'

this_year = datetime.today().year
range_of_years = range(this_year - 40, this_year + 1)
descending_range = sorted(range_of_years, reverse=True)
_get_years = [(str(x), str(x)) for x in descending_range]
year_selection = fields.Selection(_get_years, string="Select Year")


 Thank you.

Avatar
Discard
Author

It worked! thank you so much.

Related Posts Replies Views Activity
4
Dec 23
20431
5
Jul 24
13732
1
Jun 22
25198
9
May 22
50521
0
Jul 20
1878