Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error if ImageField or FileField is optional #5

Open
suryadana opened this issue Mar 8, 2019 · 0 comments
Open

Error if ImageField or FileField is optional #5

suryadana opened this issue Mar 8, 2019 · 0 comments

Comments

@suryadana
Copy link

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.

suryadana added a commit to suryadana/djadmin that referenced this issue Mar 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant