Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

How to set the current date (today date) as default for Date field using API V8 ?

Subscribe

Get notified when there's activity on this post

This question has been flagged
5 Replies
113599 Views
Avatar
Mohssin Bouktaib


0
Avatar
Discard
Avatar
Yenthe Van Ginneken (Mainframe Monkey)
Best Answer

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

18
Avatar
Discard
Mohssin Bouktaib
Author

Working Perfectly Thank you Yenthe

Dr Obx

It actually doesn't matter how, important thing is that it works ;) 1UP-vote for Yanthe

Dr Obx

I would love to give 1up for Fekete as well but I can't :)

Ermin Trevisan

@Obx : now you can :-)

Rohit Pandey

Hey @Yenthe how to get first day to month and last day of month as default.

Andreas Stauder

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

dntrplytch

only the answer posted by Andreas Stauder works for me.

Miro

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.

Nathan Marotte (nama)

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()

Mustufa Vora

Not working in website form!!

Avatar
NextERP Romania SRL
Best Answer

Hi Yenthe, isn't is easier to do a default=fields.Date.today(), without any other import...

15
Avatar
Discard
Yenthe Van Ginneken (Mainframe Monkey)

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!

Avatar
Mohssin Bouktaib
Author Best Answer

@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])

3
Avatar
Discard
Avatar
Ray Carnes (ray)
Best Answer

You can also define a default of today for any date field via context of the Window Action:

{'default_x_date': context_today()}

This is normally used in Odoo to support filters in Search Views, where it needs to be converted by adding .strftime('%Y-%m-%d') but works just fine in Window Actions as I have shown.


0
Avatar
Discard
Avatar
tuyenvn
Best Answer

1UP-vote for Yenthe. Thank you so much

0
Avatar
Discard
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now