简体   繁体   English

无法读取Django FileField?

[英]Unable to read Django FileField?

I am trying to read from Django Filefield , as can be seen in my Django Model: 我试图从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)

But os.path.isfile() keeps returning False . 但是os.path.isfile()一直返回False How do I read from FileField, into my custom model ? 如何从FileField读取我的自定义模型?

And moreover, the line fixedInfo , gives me the error: 而且, fixedInfo行给了我错误:

pywintypes.error: (2, 'GetFileVersionInfo:GetFileVersionInfoSize', 'The system cannot find the file specified.') pywintypes.error:(2,'GetFileVersionInfo:GetFileVersionInfoSize','系统找不到指定的文件。')

os.path.isfile returns whether the filepath is pointing to a file (as opposed to a directory, for instance). os.path.isfile返回文件路径是否指向文件(例如,与目录相对)。 File.file is pointing to a models.FileField object; File.file指向models.FileField对象; the current code will always return False . 当前代码将始终返回False I suppose you would want File.file.path to get the actual absolute filepath for the file. 我想你会希望File.file.path获取文件的实际绝对文件路径。

In your model definition you may add: 在您的模型定义中,您可以添加:

class File(models.Model):
    file = models.FileField()
    ...
    ...

    def filename(self):
        return os.path.basename(self.file.name)

Or you may try: 或者您可以尝试:

from django.core.files.storage import default_storage 来自django.core.files.storage import default_storage

Use: 1) FieldFile.name: 使用:1)FieldFile.name:

The name of the file including the relative path from the root of the Storage of the associated FileField. 文件名,包括相关FileField的Storage根目录的相对路径。

2) default_storage.exists(path) 2)default_storage.exists(path)

Return True if a file referenced by the given name already exists in the storage system, or False if the name is available for a new file. 如果存储系统中已存在由给定名称引用的文件,则返回True;如果该名称可用于新文件,则返回False。

Hope this works! 希望这个有效!

As you are using a different storage provider for your files, you need to use the methods of that storage provider to query the file object. 在为文件使用不同的存储提供程序时,需要使用该存储提供程序的方法来查询文件对象。

os.path.isfile and win32api.GetFileVersionInfo only work for the local file system. os.path.isfilewin32api.GetFileVersionInfo仅适用于本地文件系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM