Django:如何设置图像的大小?(models.ImageField)



我使用的是Django 1.5.

如何设置图像的大小?例如,用户必须添加超过200 * 200的图片。

models.py:

class UserNew(models.Model):
    photo = models.ImageField(upload_to='photo')

你可以重写表单的clean方法。

class UserNewForm(forms.ModelForm):
     def clean_photo(self):
         photo = self.cleaned_data.get('photo',None)
         if not photo:
             raise ValidationError("Something went wrong")
         if photo._height < 200 or photo._width < 200:
             raise ValidationError("Photo dimensions are too small (minimum 200X200 )")
         return photo

最新更新