值错误。。“image”属性没有与i关联的文件

2024-09-29 17:24:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Django 1.11.11/python2.7.5 我有一个照片模型在形式上传多个图片。此模型有错误:https://ibb.co/m3XLDn

照片型号:

class Photo(models.Model):
    produit = models.CharField(
        _("Produit (code utile pour le tri des choix):"), 
        max_length=2000, 
        blank=True, 
        null=False
        )
    legende = models.CharField(
        _("Légende:"), 
        max_length=200, 
        blank=True, 
        null=False
        )
    image = models.ImageField(
        upload_to='fiche_image', 
        blank=True, 
        null=False
        )

    def __str__(self):
        return self.produit or "undefined"
        # return str(self.produit)

    class Meta:
        ordering = ['produit', 'legende', 'image']

我不知道是什么引起了这个问题。在

新照片视图:

^{pr2}$

Tags: 模型imageselffalsetruemodelsnulllength
1条回答
网友
1楼 · 发布于 2024-09-29 17:24:09

您没有与Photo对象关联的文件。因此,它抛出了一个错误,它说“'image'属性没有与之关联的文件”。因为字段image允许存储空值。在这种情况下,Photo对象将没有关联的文件。在

你可以像下面这样检查

obj = Photo.objects.get(pk=<some int>)
if obj.image:
    print(obj.image.url)
else:
    print("No image assigned to object")

相关问题 更多 >

    热门问题