ModelAdmin
クラスのフォームをオーバーライドして、画像の寸法を検証できます。
from django.contrib import admin
from django import forms
from django.core.files.images import get_image_dimensions
from .models import ModelWithImageField
class ModelWithImageFieldForm(forms.ModelForm):
class Meta:
model = ModelWithImageField
fields = '__all__'
def clean_photo(self):
photo = self.cleaned_data["photo"]
width, height = get_image_dimensions(photo.file)
if width < 900 or height < 900:
raise form.ValidationError("Improper size.")
return photo
@admin.register(models.ModelWithImageField)
class ModelWithImageFieldAdmin(admin.ModelAdmin):
form = ModelWithImageFieldForm