0

我正在尝试将图像添加到对象。下面的一段代码

event_obj = Event.objects.get_or_create(name=name, description=description, publish=publish,
                                                url=url, venue=venue_obj, image=None)

        if image_filename:
            image = None
            image_exists = False
            if image_filename != "":
                image_path = os.path.join(settings.DIRNAME, 'website', 'data', 'images', image_filename)
                if os.path.exists(image_path):
                    image_exists = True

            if image_exists:
                image_data = open(image_path, 'rb').read()
                image, created = Image.objects.get_or_create(title=name)
                event_obj.image.save(image_filename, ContentFile(image_data), True)

        event_obj.save()

抛出以下错误

event_obj.image.save(image_filename, ContentFile(image_data), True)AttributeError: 'tuple' object has no attribute 'image'

我的事件模型使用以下字段

image = models.ImageField(null=True, blank=True, upload_to="event_images")

谁能解释为什么我不能这样保存数据,或者可能将我指向可行的方法?谢谢!

4

1 回答 1

1

这与图像无关。

get_or_create返回 (object, created) 的元组。您已将整个元组分配给event_obj.

于 2013-10-22T17:08:23.283 回答