Skip to content

Commit

Permalink
Properly handle dates when serializing models to JSON in readmodels
Browse files Browse the repository at this point in the history
  • Loading branch information
jamalex committed Aug 5, 2015
1 parent fa7f18b commit 967bca1
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion kalite/testing/management/commands/readmodel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import importlib
import json
import sys
Expand All @@ -9,6 +10,8 @@
from django.core.management.base import BaseCommand, CommandError
from django.core import serializers

dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None

class Command(BaseCommand):
args = "<model_path>"

Expand Down Expand Up @@ -40,7 +43,7 @@ def handle(self, *args, **options):
serialized_data = serializers.serialize("python", [data])[0]["fields"]
serialized_data["id"] = model_id
logging.debug("Serialized data: '%s'" % serialized_data)
print json.dumps(serialized_data)
print json.dumps(serialized_data, default=dthandler)

except Model.DoesNotExist:
logging.error("Could not find '%s' entry with primary key: '%s'" % (model_path, model_id))
Expand Down

0 comments on commit 967bca1

Please sign in to comment.