Как загрузить сохранить изображение в модели Django из файла csv

У меня проблемы с сохранением изображения в ImageField из CSV файла. Я могу сохранить все, кроме изображения, и я понятия не имею, что делать. views.py

def read_csv_file(request):
    if request.method == "POST":
        uploaded_file = request.FILES['csv_file']
        fs = FileSystemStorage()
        filename = fs.save(uploaded_file.name, uploaded_file)
        
        file =  open('C:/Fiverr projects/GrimBlog-main/media/' + uploaded_file.name)
        reader = csv.reader(file)
        for _ in range(1):
            next(file)
    
        for row in reader:
            title = row[0]
            author = request.user
            views = row[2]
            description = row[3] 
            timestamp =  strftime("%Y-%m-%d %H:%M:%S", gmtime())
            slug = row[5]
            short_description = row[6]
            publish_on = strftime("%Y-%m-%d %H:%M:%S", gmtime())
            path = row[8] 
            image = Image.open(path)

            row_9 = str(row[9])
            row_10 = (row[10])
            if row_9 == "TRUE":
                featured = True
            if row_9 == "FALSE":   
                featured = False
            
            if row_10 == "TRUE":
                case_study = True
            if row_10 == "FALSE":   
                case_study = False
            
            create_blog = Post(title = title, author = author, views = views, description = description, timestamp = timestamp, slug = slug, short_description = short_description
            , publish_on = publish_on, heading_image = image, featured = featured, case_study = case_study).save()


    return render(request, 'blog/upload_blogs.html')

models.py

class Post(models.Model):
 
title = models.CharField(max_length=30)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
views = models.IntegerField(default=0) 
description = RichTextUploadingField(blank=True, null=True) 
timestamp = models.DateTimeField(blank=True, auto_now_add=True)
slug = models.CharField(max_length=33, unique=True)
short_description = models.TextField(max_length=400)
publish_on = models.DateTimeField()
heading_image = models.ImageField(blank=True)
featured = models.BooleanField(default=False)
case_study = models.BooleanField(default=False)

def __str__(self) : 
    return self.title + ' by ' + self.author.name

def save(self, *args, **kwargs):
    if not self.slug:
        self.slug = slugify(self.title)
    return super(Post, self).save(*args, **kwargs)

В csv файле у меня есть Image root, но если кто-то знает, как загрузить изображение из csv, это тоже было бы очень полезно.

Вернуться на верх