[Answered ]-Override Djange ImageField extension validation

1👍

The problem is not the model field, but the form field. The form field has a default validator that lists all the extensions PIL supports.

You can make a special form field ModifiedImageField and specify that for the ModelForm that will be used by the MyModelAdmin in this case:

from django.contrib import admin
from django.core.validators import FileExtensionValidator
from django import forms

image_validator = FileExtensionValidator(
    allowed_extensions=['png'],
    message='File extension not allowed. Allowed extensions include  .png'
)

class ModifiedImageField(forms.ImageField):
    default_validators = [image_validator]

class MyModelAdminForm(forms.ModelForm):
    imagefield = ModifiedImageField()

class MyModelAdmin(admin.ModelAdmin):
    form = MyModelAdminForm

where imagefield is the name of the ImageField for which you want to replace the validator.

Leave a comment