Home > other >  Odoo 13: Error - wrong date format in Odoo 13
Odoo 13: Error - wrong date format in Odoo 13

Time:09-17

I am just a student gaining experience with coding in Odoo .I want to have the last day of month, current month and year as default value for my date field. I dont know what format odoo needs. Its very important. Excuse me for bad english and for unnecessary details but otherwise i cant post this

I get this error:

Error:
Odoo Server Error

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 624, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 310, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "/usr/lib/python3/dist-packages/odoo/tools/pycompat.py", line 14, in reraise
    raise value
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 669, in dispatch
    result = self._call_function(**self.params)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 350, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/odoo/service/model.py", line 94, in wrapper
    return f(dbname, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 339, in checked_call
    result = self.endpoint(*a, **kw)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 915, in __call__
    return self.method(*args, **kw)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 515, in response_wrap
    response = f(*args, **kw)
  File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 1327, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 1319, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/usr/lib/python3/dist-packages/odoo/api.py", line 385, in call_kw
    result = _call_kw_model_create(method, model, args, kwargs)
  File "/usr/lib/python3/dist-packages/odoo/api.py", line 365, in _call_kw_model_create
    result = method(recs, *args, **kwargs)
  File "<decorator-gen-153>", line 2, in create
  File "/usr/lib/python3/dist-packages/odoo/api.py", line 314, in _model_create_single
    return create(self, arg)
  File "/usr/lib/python3/dist-packages/odoo/addons/hr_timesheet/models/project.py", line 53, in create
    return super(Project, self).create(values)
  File "<decorator-gen-149>", line 2, in create
  File "/usr/lib/python3/dist-packages/odoo/api.py", line 314, in _model_create_single
    return create(self, arg)
  File "/usr/lib/python3/dist-packages/odoo/addons/project/models/project.py", line 318, in create
    project = super(Project, self).create(vals)
  File "<decorator-gen-103>", line 2, in create
  File "/usr/lib/python3/dist-packages/odoo/api.py", line 314, in _model_create_single
    return create(self, arg)
  File "/usr/lib/python3/dist-packages/odoo/addons/mail/models/mail_alias.py", line 219, in create
    record = super(AliasMixin, self.with_context(
  File "<decorator-gen-109>", line 2, in create
  File "/usr/lib/python3/dist-packages/odoo/api.py", line 335, in _model_create_multi
    return create(self, [arg])
  File "/usr/lib/python3/dist-packages/odoo/addons/mail/models/mail_thread.py", line 269, in create
    threads = super(MailThread, self).create(vals_list)
  File "<decorator-gen-3>", line 2, in create
  File "/usr/lib/python3/dist-packages/odoo/api.py", line 336, in _model_create_multi
    return create(self, arg)
  File "/usr/lib/python3/dist-packages/odoo/models.py", line 3754, in create
    vals = self._add_missing_default_values(vals)
  File "/usr/lib/python3/dist-packages/odoo/models.py", line 1792, in _add_missing_default_values
    defaults = self.default_get(list(missing_defaults))
  File "/usr/lib/python3/dist-packages/odoo/models.py", line 1262, in default_get
    value = field.convert_to_cache(value, self, validate=False)
  File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1741, in convert_to_cache
    return self.to_date(value)
  File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1716, in to_date
    return datetime.strptime(value, DATE_FORMAT).date()
  File "/usr/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/usr/lib/python3.8/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '_set_defau' does not match format '%Y-%m-%d'

This is my code :

from datetime import timedelta, datetime
from calendar import monthrange

class whatever(models.Model):

end_date = fields.Date(
        'End date', default='_set_default_date')

def _set_default_date(self):
        mdays = monthrange(year, month)[1]
        return date(year, month, mdays)

CodePudding user response:

Try this

from odoo.tools import date_utils

class whatever(models.Model):

 end_date = fields.Date( 'End date', default=date_utils.end_of(fields.Date.today(), 'month'))



  • Related