У объекта 'str' нет атрибута 'chunks'

У меня такая ошибка при попытке сохранить изображение в папке и в mongo db как я могу решить эту проблему

def save_img_field(value):

    # Save image file to the static/img folder
    image_path = save_image_to_folder(value)

    # Save image to MongoDB using GridFS
    image_id = fs.put(value, filename=value, content_type=value.content_type)

    # Return the image id and path for storage in MongoDB and Django folder
    return {'id': str(image_id), 'path': image_path}


def save_image_to_folder(value):
    # Create the file path to save the image in the Django static/img folder
    image_name = value
    image_path = f'decapolis/static/img/{image_name}'

    # Open the image file and save it to the folder
    with open(image_path, 'wb+') as destination:
        for chunk in value.chunks():
            destination.write(chunk)

    # Return the image path
    return image_path

Я пытаюсь решить это многими способами, но не получается

При сохранении в файл добавьте проверку, является ли он строкой или изображением:

with open(image_path, 'wb+') as destination:
    if type(value) == str:
        destination.write(value)   # or whatever you actually want to do if it's a string
    else:
        for chunk in value.chunks():
            destination.write(chunk)
Вернуться на верх