[英]django admin custom form validation
I am trying to validate image in django admin using form validation. 我正在尝试使用表单验证在django admin中验证图像。
models.py 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 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. 我看到了其他答案,并且可以确保这些要点,但是当我上传违反验证规则的图像时,django仍然不会引发任何验证错误。
The field name is screenshot . 字段名称是screenshot 。 clean_image never gets executed.
clean_image永远不会执行。 You need to change the method name to clean_screenshot .
您需要将方法名称更改为clean_screenshot 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.