Django Populate DateTimeField validation Error

how recepes

This is mym models:

class Post(models.Model):
    timestamp = models.DateTimeField()

I am trying to populate this field like this:

Post.objects.create(timestamp='20-03-20 8:56')

and throws me following error:

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

Can anyone help me to fix this?

Willem Van Onsem

As specified, the format is:

YYYY-MM-DD  HH:MM[:ss[.uuuuuu]][TZ]

Hence you can construct this with:

Post.objects.create(timestamp='2020-03-20 8:56')

It might however be more convenient to pass a datetime object:

from datetime import datetime
from pytz import UTC

Post.objects.create(timestamp=datetime(2020, 3, 20, 8, 56, tzinfo=UTC))

You can of course select another timezone instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related