What is proper way to store data in DateTimeField field

I have a model Fixture

Class Fixture(models.Model):
     event_timestamp = models.DateTimeField(null=True)

I have piece of json data

for item in piece_json:
    event_timestamp = item["event_date"]

Where in item["event_timestamp"] the following data 1572567600

While i am trying to create object from this model

fixture = Fixture.objects.create(event_timestamp= event_timestamp)

I got the following error

match = datetime_re.match(value)
TypeError: expected string or bytes-like object

After this error i tried to wrap event_timestamp variable in built-in str() function

fixture = Fixture.objects.create(event_timestamp= str(event_timestamp))

After it i got error

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

Upvotes: 0

Views: 40

Answers (1)

rossi
rossi

Reputation: 314

I don't think DateTimeField allowsyou to save Unix Timestamp.

What I can suggest you do is:

import datetime
fixture = Fixture.objects.create(event_timestamp=datetime.datetime.utcfromtimestamp(event_timestamp))

Upvotes: 3

Related Questions