关于Imagefield的困惑

时间:2020-09-02 16:54:16

标签: django django-models

我正在学习Django入门。通常来讲(从文档角度而言),Imagefield是否用于照片?那么图像文件的哪个字段呢? (例如dmg)。谢谢。

1 个答案:

答案 0 :(得分:1)

ImageField继承了FileField的所有属性和方法,但也使用Pillow验证了上载的对象是有效的图像。它还有两个附加的可选参数:height_fieldwidth_field(每次保存模型实例时,它们分别自动填充图像的高度和宽度)。

示例:

class Photo(models.Model):
    image = models.ImageField(blank=True, null=True, upload_to='image_collection', height_field='image_height', width_field='image_width', max_length=1000)
    image_height = models.PositiveIntegerField(null=True, blank=True, editable=False)
    image_width = models.PositiveIntegerField(null=True, blank=True, editable=False)
相关问题