How to show a year in the Integer field w/o comma between order groups?

Good morning,

I am a little puzzled by the Date field.
I need to store a year only (1998, 2008, 2020, etc). I will use that as a number.
I don’t want a user to use a date widget to select the year, just typing 2017 would be fine.
But if I use the Integer field, it displays like “1,998” “2,008” or “2,020” — a kind weird.
How would you recommend to implement this function?

Thank you,
PS: Using strings… I don’t want people typing “fgfgfg” instead of a year, numbers only allowed. I don’t want commas or pesky “select the date” widgets…
Maybe, I can use String type and check string with year.isdecimal() with on_change method?

If you want to store a year only the best option is to use a datetime but with an specific format. This can be set by setting the required format in the ‘date_format’ key of the field contex. If you want to use the year only you should use the following value: ‘’%Y’

There is an example on the spanish localization module.



I wrote it in the following way:

prod_year = fields.Date("Production year", required=False,
    context={'date_format': '%Y'})
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.