Skip to content

Error if ImageField or FileField is optional #5

@suryadana

Description

@suryadana

I think on the function get_file_detail must be checking file is None when class name ImageField or FileField.

If you can implement my code as below.


@register.simple_tag
def get_file_detail(adminform, field):
    field_data = adminform.form.initial[field]
    field_name = type(field_data).__name__
    try:
       if field_name == 'ImageFieldFile':
            if not bool(field_data):
                return {'type': 'image', 'width': 0, 'height': 0, 'url': '', 'size': 0, 'extension': ''}
            filename, file_extension = os.path.splitext(field_data.url)
            return {'type': 'image', 'width': field_data.width, 'height': field_data.height, 'url': field_data.url, 'size': field_data.size, 'extension': file_extension}
       if field_name == 'FieldFile':
           if not bool(field_data):
               return {'type': 'file', 'url': '', 'size': 0, 'extension': ''}
           filename, file_extension = os.path.splitext(field_data.url)
           return {'type': 'file', 'url': field_data.url, 'size': field_data.size, 'extension': file_extension}
       return field
    except IOError:
        pass

Thank You.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions