I'm trying to serialize one of my models which has an ImageField
. The inbuilt serializer can't seem to serialize this and therefore I thought of writing a custom serializer. Could you tell me how I could serialize an image and use this with the default JSON serializer in Django?
Thanks
I wrote an extension to the simplejson encoder. Instead to serializing the image to base643, it returns the path of the image. Here's a snippet:
def encode_datetime(obj):
"""
Extended encoder function that helps to serialize dates and images
"""
if isinstance(obj, datetime.date):
try:
return obj.strftime('%Y-%m-%d')
except ValueError, e:
return ''
if isinstance(obj, ImageFieldFile):
try:
return obj.path
except ValueError, e:
return ''
raise TypeError(repr(obj) + " is not JSON serializable")