Reputation: 2614
I am trying to validate image in django admin using form validation.
models.py
class website_index(models.Model):
screenshot=models.ImageField(storage=AzureStorage(container="media"),upload_to='search/resources/screenshots/',blank=True,null=True)
admin.py
class CustomIndexModelForm(forms.ModelForm):
class Meta:
model = website_index
fields = '__all__'
def clean_image(self):
screenshot = self.cleaned_data.get['screenshot']
if screenshot:
w, h = get_image_dimensions(screenshot)
if w!=h :
raise forms.ValidationError('Upload a square image')
return screenshot
def __init__(self, *args, **kwargs):
super(CustomIndexModelForm, self).__init__(*args, **kwargs)
self.fields['parent_id'].queryset = cat_tree.objects.exclude(level=0)
class website_index_Admin(admin.ModelAdmin):
form = CustomIndexModelForm
I have seen other answers and I am ensuring those points, still django is not raising any Validation error when I upload image that violate the validation rules.
Upvotes: 0
Views: 3356
Reputation: 76
The field name is screenshot. clean_image never gets executed. You need to change the method name to clean_screenshot.
Upvotes: 5