[Answered ]-Json.dumps in python over a list of user-defined objects

2👍

You can create a custom JSON Encoder class that knows how to encode ImageFieldFile:

import json
from django.db.models.fields.files import ImageFieldFile

class EnhancedJSONEncoder(json.JSONEncoder):
    """
    JSONEncoder subclass that knows how to encode Django "ImageFieldFile"s.
    """

    def default(self, o):
        if isinstance(o, ImageFieldFile):
            # Treat ImageFieldFile as a unicode string
            return super(EnhancedJSONEncoder, self).default(unicode(o))
        else:

            return super(EnhancedJSONEncoder, self).default(o)

You would then call json.dumps() as:

json.dumps(listData, cls=EnhancedJSONEncoder))

This assumes you’re wanting ImageFieldFile to be represented as a string

👤jcater

Leave a comment