Home > OS >  Django DateTimeField Timestamp value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuu
Django DateTimeField Timestamp value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuu

Time:11-29

The environment is:

  • Django 4.0 (venv)
  • Python 3.8
  • Postgres 15
  • Elementary OS 6.1

The initial model:

class MyModel(models.Model):
    other_field = models.BooleanField()
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

Upon saving a new record (without providing any value for "created" nor "updated") the result was:

django.core.exceptions.ValidationError: ['“1669827388000” value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.']

Then I've followed some suggestions to do this:

class MyModel(models.Model):
    created = models.DateTimeField(editable=False)
    updated = models.DateTimeField(editable=False)

# Still doesnt work: It gets a timestamp
def save(self, *args, **kwargs):
    if not self.id:
        self.created = timezone.now()
    self.updated = timezone.now()
    return super(Token, self).save(*args, **kwargs)

The result was exactly the same (of course the timestamp's value changed).

I've tried variations with django.settings settings.USE_TZ from True to False & vice versa.

Traceback:

  Traceback (most recent call last):
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/views/generic/base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/rest_framework/decorators.py", line 50, in handler
    return func(*args, **kwargs)
  File "/home/PROJECT_NAME/app_name/views.py", line 39, in register_new_email
    new_token.save()
  File "/home/PROJECT_NAME/app_name/models.py", line 170, in save
    return super(Token, self).save(*args, **kwargs)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/base.py", line 812, in save
    self.save_base(
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/base.py", line 863, in save_base
    updated = self._save_table(
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/base.py", line 1006, in _save_table
    results = self._do_insert(
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/base.py", line 1047, in _do_insert
    return manager._insert(
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/query.py", line 1790, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1659, in execute_sql
    for sql, params in self.as_sql():
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1583, in as_sql
    value_rows = [
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1584, in <listcomp>
    [
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1585, in <listcomp>
    self.prepare_value(field, self.pre_save_val(field, obj))
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1523, in prepare_value
    value = field.get_db_prep_save(value, connection=self.connection)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 925, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1582, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1560, in get_prep_value
    value = super().get_prep_value(value)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1433, in get_prep_value
    return self.to_python(value)
  File "/home/PROJECT_NAME/.venv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1542, in to_python
    raise exceptions.ValidationError(
django.core.exceptions.ValidationError: ['“1669827388000” value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.']

Thanks in advance for any suggestion!

EDIT

The insertion is done in a view I just don't insert anything at the DateTime fields of the model & django -supposedly- handles it automatically:

@api_view(['GET'])
def dummy_view(request):
    new_record = MyModel(
        other_field=True
    )
    new_token.save()

CodePudding user response:

As @AdrianKlaver mentioned, a conversion from ms should take place first.

  • Related