I recently upgraded a Django project to version 1.8, and I am happily using Postgresql and Django’s new
UUIDField. While using django-eztables to do the server-side work for DataTables.net, I ran into an issue with a UUID field.
Exception Type: TypeError
Exception Value: UUID('ff6ae150-e218-11e4-8146-bc764e04b86f') is not JSON serializable
After some reason, I found out that
json doesn’t support a plug-gable architecture, so the only transparent way to fix this was moneypatching. I put the following into my models.py file.
''' Dealing with no UUID serialization support in json ''' from json import JSONEncoder from uuid import UUID JSONEncoder_olddefault = JSONEncoder.default def JSONEncoder_newdefault(self, o): if isinstance(o, UUID): return str(o) return JSONEncoder_olddefault(self, o) JSONEncoder.default = JSONEncoder_newdefault
Hope this helps others.