I followed How to overcome "datetime.datetime not JSON serializable" in python? but this is not helping
I tried this code
>>> import datetime
>>> a =datetime.date(2014, 4, 25)
>>> from bson import json_util
>>> b = json.dumps(a,default = json_util.default)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.7/json/__init__.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/home/.../python2.7/site-packages/bson/json_util.py", line 256, in default
raise TypeError("%r is not JSON serializable" % obj)
TypeError: datetime.date(2014, 4, 25) is not JSON serializable
Can somebody help me with a datetime.date
serializer and deserializer.
You can add a date time encoder into the JSON jumps function when handling model querysets, this is customised a bit as I had issues with the base django model state being parsed
Then use this class with your json dumps
I've found this to be invaluable, especially after updating Django from 1.7 to 1.9. Most of this is from the blog http://arthurpemberton.com/2015/04/fixing-uuid-is-not-json-serializable Put this in models.py right under the imports. It'll take care of UUIDs for you too.
Convert date to equivalent iso format,
See the Extending encoder section from the json package docs https://docs.python.org/2/library/json.html
I have used this method and found it quite effective. I think this is what you are looking for.
You can also do this:
From here.
Update as per J.F.Sebastian comment