【问题标题】:ImageField image_width and image_height auto fill for existing database?现有数据库的 ImageField image_width 和 image_height 自动填充?
【发布时间】:2018-04-14 07:55:37
【问题描述】:

我有一个现有的数据库表,其中 ImageField 已经有一堆现有的注册表。我想在我的模型上添加image_widthimage_height,但我需要一些东西来自动填充现有的表格。

class ItemImage(models.Model):
    file = models.ImageField(upload_to=image_directory_path, width_field='image_width', height_field='image_height')
    image_width = models.IntegerField()
    image_height = models.IntegerField()

我在文档中看到image_widthimage_heightsave() 上时会自动生成,但现有图像已经保存。

【问题讨论】:

    标签: django python-3.x django-models


    【解决方案1】:

    显然,我找到了解决方案!这是一个奇怪的,但它的工作原理。我将 image_widthimage_height 都设置为默认为 0 并在 dev 上进行了标准迁移。

    当然,您会假设所有现有图像都设置为0 高度和宽度,但是当我尝试访问这些属性时,例如,在它们的详细视图中,我可以访问正确的高度和宽度值原始图像(在我进行迁移之前保存的现有图像)。

    如果有人能解释一下,我将不胜感激。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-25
      • 2014-12-28
      • 2022-01-19
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      相关资源
      最近更新 更多