python - 无法读取 Django FileField?
问题描述
我正在尝试从Django Filefield读取,如我的 Django 模型中所示:
import os
import win32api
from django.db import models
from custom.storage import AzureMediaStorage as AMS
class File(models.Model):
'''
File model
'''
file = models.FileField(blank=False, storage=AMS(), null=False)
timestamp = models.DateTimeField(auto_now_add=True)
remark = models.CharField(max_length=100, default="")
class File_Version(File):
"""
Model containing file version information
"""
version = models.CharField(max_length=25, default="")
@property
def get_version(self):
"""
Read all properties of the given file and return them as a dictionary
"""
props = {'FileVersion': None}
# To check if the file exists ?
### This returns FALSE
print("Is the file there? ", os.path.isfile(str(File.file)) )
# To get file version info
fixedInfo = win32api.GetFileVersionInfo(str(File.file), '\\')
print("FixedInfo: ", fixedInfo)
但是os.path.isfile()不断返回False。如何从 FileField 读取到我的自定义模型?
此外,fixedInfo行给了我错误:
pywintypes.error: (2, 'GetFileVersionInfo:GetFileVersionInfoSize', '系统找不到指定的文件。')
解决方案
os.path.isfile
返回文件路径是否指向文件(例如,与目录相反)。 File.file
指向一个models.FileField
对象;当前代码将始终返回False
。我想您会想要File.file.path
获取文件的实际绝对文件路径。
推荐阅读
- swift - 在 Swift 中显示操作系统
- javascript - 尝试从传单地理编码器返回纬度和经度值
- strapi - 特定字段的 Strapi 总和值
- java - 如何通过 Maven 运行 Stilliard/pure-ftpd
- swift - 将包写为文件
- javascript - 使用 App 首字母缩写词为所有类/接口/函数添加前缀是一种好习惯吗?
- amazon-web-services - 监控 EKS Kubernetes LoadBalancer 服务类型
- android - 以下构建成功消息后如何解决“错误:未找到构建工件”的错误?
- javascript - 浏览器放大/缩小的提示工具提示位置
- c - 打印 int 值或将 int 转换为 C 语言中的字符串