我正在尝试将图像保存在文件夹中,并使用ForeignKey将其绑定到另一个数据库对象,因此以后可以查找它。我有抓取文件夹中图像的代码,现在我只是想保存它们。
我当前的错误是:
'ImageFileDescriptor' object has no attribute 'save'
Models.py
class Usertasks(models.Model):
TaskID = models.CharField(max_length=55)
user = models.ForeignKey(User, unique=False, on_delete=models.CASCADE)
TaskStatus = models.CharField(max_length=15, default="missing")
class TaskImages(models.Model):
UserTasks = models.ForeignKey(Usertasks, related_name='images', on_delete=models.CASCADE)
image = models.ImageField()
views.py
def task_info(request, taskid):
task = Usertasks.objects.get(TaskID=taskid)
files = os.listdir(task.OutputPath)
fullpath = task.OutputPath
print(files)
for img in files:
fullpath = os.path.abspath(img)
with open(fullpath, 'rb') as f:
data = File(f)
TaskImages.image.save(img, data, True, UserTasks=task)
return render(request, 'dashboard/task.html', {'path':fullpath, 'image':files})