Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Accounting
- Inventory
- PoS
- Project management
- MRP
This question has been flagged
Hi Moubou,
Import the correct Python library for date(times) at the top of your Python file:
from datetime import datetime
and then create the field in the model with the date pre-filled:
your_date_field = fields.Date(string='Your string', default=datetime.today())
This will automatically fill in the today's date in the date field.
Yenthe
Working Perfectly Thank you Yenthe
It actually doesn't matter how, important thing is that it works ;) 1UP-vote for Yanthe
I would love to give 1up for Fekete as well but I can't :)
@Obx : now you can :-)
Hey @Yenthe how to get first day to month and last day of month as default.
Just for the records, the answer is not correct. With this solution it takes always the date of the day the server was started, see https://github.com/odoo/odoo/issues/20493
Correct would be
your_date_field = fields.Date(string='Your string', default=lambda self: fields.Date.today())
As can be found e.g. in https://github.com/odoo/odoo/blob/b9abc757da3034209b083c4ba862edd3865f7637/odoo/addons/base/models/res_currency.py#L238
only the answer posted by Andreas Stauder works for me.
Absolute lie. This does not work and failed me in a project. Administrators must mark this answer as wrong.
default=datetime.today() returns the date when Odoo service is started. If you restart odoo to test the date seems today but if you try few days later the date will be in past.
This will indeed set the default as today - when the code was executed, and not today - when data is registered.
To circumvent this, you can pass a function to the default that will be called every time a default is requested. This is done by specifying default=lambda _ : datetime.today() instead of default=datetime.today()
Hi Yenthe, isn't is easier to do a default=fields.Date.today(), without any other import...
Agreed that this would be a neat way in order to not use any import. Usually you do need the datetime import for a few things when you've added date fields though. +1 for you!
@Rohit Pandey Hello Rohit,
You can try this
first_day_of_month = fields.Date(string='First Day Month', required=True,
default=datetime.now().strftime('%Y-%m-01'))
last_day_of_month = fields.Date(string='Last Day Month',
default=str(datetime.now() + relativedelta.relativedelta(months=+1, day=1, days=-1))[:10])
1UP-vote for Yenthe. Thank you so much