Django ImageField设置固定的宽度和高度

Django ImageField设置固定的宽度和高度,django,Django,我的models.py中有以下图像字段(请参见下面的代码) 我想设置图像的固定宽度和高度,使其始终为100x100px 下面的代码是系统中已经存在的代码,我不确定如何传递宽度和高度,或者是否可以使用此代码将宽度和高度设置为固定大小 image = models.ImageField( upload_to="profiles", height_field="image_height", width_field="image_width",

我的models.py中有以下图像字段(请参见下面的代码)

我想设置图像的固定宽度和高度,使其始终为100x100px

下面的代码是系统中已经存在的代码,我不确定如何传递宽度和高度,或者是否可以使用此代码将宽度和高度设置为固定大小

image = models.ImageField(
        upload_to="profiles",
        height_field="image_height",
        width_field="image_width",
        null=True,
        blank=True,
        editable=True,
        help_text="Profile Picture",
        verbose_name="Profile Picture"
    )
    image_height = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")
    image_width = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")

您想在首次上载图像时缩小图像的大小,还是始终以100x100显示图像?另外,您确定图像的纵横比都是1:1吗?如果没有,您可能希望在上载时调整大小,并将较大的维度设置为100,并保留纵横比。我希望在上载时将图像大小重新调整为100x100,而不是在显示时缩小,很抱歉,我本应澄清这一点。可能希望取消标记下面的答案。应该向您展示如何在视图中执行此操作,这提供了另一种方法。我通常在模型的save()方法中执行此操作。我是否可以将上载的图像大小更改为100x100?我应该说清楚的,对不起。
class ModelName(models.Model):    
    image = models.ImageField(
        upload_to="profiles",
        null=True,
        blank=True,
        editable=True,
        help_text="Profile Picture",
        verbose_name="Profile Picture"
    )
    image_height = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")
    image_width = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")

    def __unicode__(self):
        return "{0}".format(self.image)

    def save(self):
        if not self.image:
            return            

        super(ModelName, self).save()
        image = Image.open(self.photo)
        (width, height) = image.size     
        size = ( 100, 100)
        image = image.resize(size, Image.ANTIALIAS)
        image.save(self.photo.path)