hello,
how to restrict field date to get only year?
for exemple, in { start_year = fields.Date() } i'ld like to get 2019 instead of 11:02:22 2019/12/31
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
hello,
how to restrict field date to get only year?
for exemple, in { start_year = fields.Date() } i'ld like to get 2019 instead of 11:02:22 2019/12/31
If you want to add field in which users selects a year: make a selection field. An example for a year from 2000 to 2030:
@api.model
def year_selection(self):
year = 2000 # replace 2000 with your a start year
year_list = []
while year != 2030: # replace 2030 with your end year
year_list.append((str(year), str(year)))
year += 1
return year_list
year = fields.Selection(
year_selection,
string="Year",
default="2019", # as a default value it would be 2019
)
If you want to retrieve a year from a date variable:
start_year.year #odoo 12
fields.Date.from_string(start_year).year #earlier versions
Have a look at the Python datetime documentation - https://docs.python.org/3/library/datetime.html
It gives error on using that function name inside the selection field
You need to call function by selection='function_name'
year = fields.Selection(
selection='years_selection',
string="Year",
default="2019", # as a default value it would be 2019
)
year_of_completion = fields.Selection(
selection='years_selection',
string="Estimated Year of Completion",
default="2021" # as a default value it would be 2019
)
def years_selection(self):
year_list = []
for y in range(datetime.now().year, datetime.now().year + 10):
year_list.append((str(y), str(y)))
return year_list
I have already defined these two and these are working fine in my localhost. But when I pushed it onto live server, it doesn't work. Can anyone help me with this?
Hi,
try this code to get year or month.
date = datetime.strptime(date_field, DEFAULT_SERVER_DATE_FORMAT)
month = date.month
year = date.year
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign up