def field_serializer(field_value):
    """
    return the field value ready to be serialized.
    depending of the field type:
        QuerySet, foreign key, file field, image field, etc
    """
    if hasattr(field_value,'get_query_set') or isinstance(field_value, QuerySet):
        # probably is a m2m field, so lets get it with serializer
        field_value = [model_to_dict(m) for m in field_value.all()]
    elif isinstance(field_value, models.Model):
        # is a foreign key, so lets get it with model_to_dict
        field_value = model_to_dict(field_value)
        for k,v in field_value.iteritems():
            field_value[k] = field_serializer(v)
    elif isinstance(field_value, InMemoryUploadedFile) or isinstance(field_value, TemporaryUploadedFile):
        try:
            # new files/images uploaded does not have a path or url already
            field_value = {
                'name': getattr(field_value, 'name',''),
                'size': getattr(field_value, 'size',''),
                'content_type': getattr(field_value, 'content_type',''),
                'charset': getattr(field_value, 'charset',''),
                'encoding': getattr(field_value, 'encoding',''),
            }
        except __HOLE__:
            field_value = force_unicode(field_value)
    elif hasattr(field_value,'path') or isinstance(field_value, FieldFile):
        # the field is an image or file. so lets get the file's 'path'
        try:
            field_value = {
                'url': getattr(field_value, 'url',''),
                'path': getattr(field_value, 'path',''),
                'size': getattr(field_value, 'size',''),
                'content_type': getattr(field_value, 'content_type',''),
                'charset': getattr(field_value, 'charset',''),
                'name': getattr(field_value, 'name',''),
                'encoding': getattr(field_value, 'encoding',''),
            }
            if field_value['path']:
                field_value['path'] = field_value['path'].split(settings.MEDIA_ROOT)[1]
        except ValueError:
            # possible the file field is empty (or deleted)
            field_value = force_unicode(field_value)
    else:
        field_value = force_unicode(field_value)
    return field_value
dataset/ETHPy150Open scieloorg/scielo-manager/scielomanager/audit_log/helpers.py/field_serializer