تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
1 الرد
5407 أدوات العرض

How to give the specific date like "2022-12-31",not the default date as today?

hire_date = fields.Date(string="Hire Date", default=fields.Date.today)
resign_date = fields.Date(string="Resign Date")

الصورة الرمزية
إهمال
أفضل إجابة
 hire_date = fields.Date(string="Hire Date", default=datetime.strptime("2022-12-31", '%Y-%m-%d').date())

You can try the above code.

الصورة الرمزية
إهمال
الكاتب

It didn't work.I tried the below three ways,still showing the error.

(1)datetime.strptime("2022-12-31", '%y-%m-%d').date();

(2)date.strptime("2022-12-31", '%y-%m-%d').date()

(3)strptime("2022-12-31", '%y-%m-%d').date()

from ast import literal_eval

from odoo import api, fields, models

from pytz import timezone, UTC

from odoo.tools import format_time

from datetime import date, datetime, timedelta, time

from time import time

import datetime

import time

import calendar

(1)

File "/opt/bitnami/apps/odoo/lib/odoo-13.0.post20191110-py3.7.egg/odoo/addons/hr/models/hr_employee_base.py", line 33, in HrEmployeeBase

hire_date = fields.Date(string="Hire Date", default=datetime.strptime("2022-12-31", '%y-%m-%d').date())

AttributeError: module 'datetime' has no attribute 'strptime'

(2)

File "/opt/bitnami/apps/odoo/lib/odoo-13.0.post20191110-py3.7.egg/odoo/addons/hr/models/hr_employee_base.py", line 33, in HrEmployeeBase

hire_date = fields.Date(string="Hire Date", default=date.strptime("2022-12-31", '%y-%m-%d').date())

AttributeError: type object 'datetime.date' has no attribute 'strptime'

(3)

File "/opt/bitnami/apps/odoo/lib/odoo-13.0.post20191110-py3.7.egg/odoo/addons/hr/models/hr_employee_base.py", line 33, in HrEmployeeBase

hire_date = fields.Date(string="Hire Date", default=("2022-12-31", '%y-%m-%d').date())

AttributeError: 'tuple' object has no attribute 'date'

from datetime import datetime

The above code should be enough for the import.

Please use capital Y to represent year i.e., use "%Y-%m-%d" in your code, I have made a mistake in the first reply.

المنشورات ذات الصلة الردود أدوات العرض النشاط
1
يناير 25
17040
1
يناير 20
3288
1
ديسمبر 19
16468
3
أبريل 18
4575
1
مايو 16
6093