S
S
stayHARD2016-03-07 13:00:57
Django
stayHARD, 2016-03-07 13:00:57

How to serialize a datetime object?

Good afternoon.
Model:

class Challenge(models.Model):
    owner = models.ForeignKey(User)
    distance = models.FloatField(default=0)
    time = models.FloatField(default=0)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()

Blizzard:
Time comes in the form: 1457342749323
new_challenge = Challenge.objects.create(owner_id=token.user_id, distance=distance, time=time,                                start_date=datetime.datetime.fromtimestamp(int(challenge["start_date"])/1000.0),                                                   end_date=datetime.datetime.fromtimestamp(int(challenge["end_date"])/1000.0))

Then, in the same view, you need to serialize the object and send GCM pushes.
new_challenge_serializer = ChallengeSerializer(new_challenge)
data = {
    "data": new_challenge_serializer.data
}
reg_id = [notification.notification_id]
gcm.json_request(registration_ids=reg_id, data=data)

I get:
TypeError: datetime.datetime(2016, 3, 7, 9, 26, 5, 152000, tzinfo=) is not JSON serializable
What's wrong with my date?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Sklyar, 2016-03-07
@VladSkliar

Create the desired property in the model and add it to the serializer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question