from django import forms from .models import Upload, Image class UploadImageForm(forms.ModelForm): class Meta: model = Image fields = ['file'] def save(self, commit=True): image = super().save(commit=False) if not image.name: image.name = image.file.name if not image.content_type: from utils import get_mimetype image.content_type = get_mimetype(image.file) image.size = image.file.size if commit: image.save() return image