在Django的ImageField中使用height_field和width_field属性

在Django的ImageField中使用height_field和width_field属性,django,django-models,Django,Django Models,我有一个模型 class Anh_chi_tiet(models.Model): du_an = models.ForeignKey(Du_an) title = models.CharField(max_length=120) url = models.ImageField(upload_to='images', height_field='50', width_field='100') url_detail = mod

我有一个模型

class Anh_chi_tiet(models.Model):
    du_an       = models.ForeignKey(Du_an)
    title       = models.CharField(max_length=120)
    url         = models.ImageField(upload_to='images', height_field='50', width_field='100')
    url_detail  = models.ImageField(upload_to='images', height_field='200', width_field='400')
我总是收到以下错误:

'Anh_chi_tiet' object has no attribute '100'
Django似乎需要同一个表中的两个字段和一个chiu tiet来保存高度字段和宽度字段


如何直接设置这些值而不在表中追加新字段?

height\u field
表示用于存储高度的模型属性

class Anh_chi_tiet(models.Model):

  url_height=models.PositiveIntegerField()
  url_width=models.PositiveIntegerField()
  url = models.ImageField(upload_to='images', height_field='url_height', width_field='url_width')

请注意
height\u field='url\u height'

我认为您误解了height\u field和width\u field属性的含义

高度_字段不用于更改高度。它用来记录已经达到的高度

这样你可以做一些事情,比如

<img src="..." height={{my_instance.my_height_field}} /> 

无需读取文件

如果您试图调整通过ImageField上传的所有图像的大小,请选中此处:

有没有办法不创建url\u高度和url\u宽度字段而直接设置高度和宽度字段?下面是@Nifled的一个解决方案,希望对您有所帮助-