Django FileField(或ImageField) open()方法没有返回有效文件?

时间:2021-08-16 19:23:51

let me put it like this:

让我这样来写:

model.py:

model.py:

class Task(models.Model):
    ...
    seq_file = models.FileField(upload_to='files/', blank=True, null=True)
    ...

ajax.py (I'm using dajaxice but it doesn't matter):

ajax。py(我用的是dajaxice,但是没关系):

...
def startTask(request, name):
    task = Task.objects.get(task_name=name)
    data = task.seq_file.open()
    filename = os.path.join(settings.MEDIA_ROOT ,task.seq_file.name)
    if not os.path.isfile(filename):
        raise Exception, "file " + filename + " not found."
    sequences = parser.parse(data.read())
    ...

this returns:

这将返回:

File "/home/mnowotka/Dokumenty/MgrFuncAdnot/app/django-gui/src/gui/ajax.py", line 43, in startTask
sequences = parser.parse(data.read())

AttributeError: 'NoneType' object has no attribute 'read'

but:

但是:

...
def startTask(request, name):
    task = Task.objects.get(task_name=name)
    filename = os.path.join(settings.MEDIA_ROOT ,task.seq_file.name)
    if not os.path.isfile(filename):
        raise Exception, "file " + filename + " not found."
    data = open(filename)  
    sequences = parser.parse(data.read())
    ...

works perfectly! Why?

作品完美!为什么?

(I'm using django 1.3)

(我使用django 1.3)

4 个解决方案

#1


25  

because open method of models.FileField doesn't return anything

因为模型的开放方法。FileField不返回任何值

you can just use:

你可以使用:

task.seq_file.read()

and you don't need calculate path of file for checking if file exist. you can use task.seq_file.path:

并且不需要计算文件路径来检查文件是否存在。您可以使用task.seq_file.path:

if not os.path.isfile(task.seq_file.path):
    ....

#2


7  

A FileField will give you a file-like object and there is no need to call open() on it. In your example, just call task.seq_file.file.

FileField会给你一个类似文件的对象,不需要调用open()。在您的示例中,只需调用task.seq_file.file。

Why is that? There are many storage backends for FileField, and many of them are not backed by a file in disk (think of S3 storage, for example). I guess that is why the documentation says it returns a file-like object, not a file. For some kinds of storage the "open" method makes no sense.

这是为什么呢?FileField有许多存储的后台,其中很多都没有磁盘上的文件支持(比如S3存储)。我猜这就是为什么文档说它返回一个类文件的对象,而不是一个文件。对于某些类型的存储,“打开”方法毫无意义。

#3


3  

When in doubt, check the code. Here's an excerpt from django.db.models.fields.files:

如果有疑问,请检查代码。以下是django.db.models.fields.files的摘录:

def open(self, mode='rb'):
    self._require_file()
    self.file.open(mode)
# open() doesn't alter the file's contents, but it does reset the pointer
open.alters_data = True

So, in the case of a FileField, open reopens the file using the specified mode. Then, once you call open, you can continue to use methods like read using the newly applied mode.

因此,对于FileField,使用指定的模式打开重新打开文件。然后,一旦调用open,就可以继续使用诸如read之类的方法,使用新应用的模式。

#4


0  

Surprisingly but django.db.models.fields.files does not utilize file.storage.exists() method so I had to implement my own small function to have cross-storage compatible check for actual physical file existence:

但django.db.models.fields得惊人。文件不使用file.storage.exists()方法,因此我必须实现自己的小函数,以便对实际的物理文件存在进行跨存储兼容检查:

# Check whether actual file of FileField exists (is not deleted / moved out).
def file_exists(obj):
    return obj.storage.exists(obj.name)

#1


25  

because open method of models.FileField doesn't return anything

因为模型的开放方法。FileField不返回任何值

you can just use:

你可以使用:

task.seq_file.read()

and you don't need calculate path of file for checking if file exist. you can use task.seq_file.path:

并且不需要计算文件路径来检查文件是否存在。您可以使用task.seq_file.path:

if not os.path.isfile(task.seq_file.path):
    ....

#2


7  

A FileField will give you a file-like object and there is no need to call open() on it. In your example, just call task.seq_file.file.

FileField会给你一个类似文件的对象,不需要调用open()。在您的示例中,只需调用task.seq_file.file。

Why is that? There are many storage backends for FileField, and many of them are not backed by a file in disk (think of S3 storage, for example). I guess that is why the documentation says it returns a file-like object, not a file. For some kinds of storage the "open" method makes no sense.

这是为什么呢?FileField有许多存储的后台,其中很多都没有磁盘上的文件支持(比如S3存储)。我猜这就是为什么文档说它返回一个类文件的对象,而不是一个文件。对于某些类型的存储,“打开”方法毫无意义。

#3


3  

When in doubt, check the code. Here's an excerpt from django.db.models.fields.files:

如果有疑问,请检查代码。以下是django.db.models.fields.files的摘录:

def open(self, mode='rb'):
    self._require_file()
    self.file.open(mode)
# open() doesn't alter the file's contents, but it does reset the pointer
open.alters_data = True

So, in the case of a FileField, open reopens the file using the specified mode. Then, once you call open, you can continue to use methods like read using the newly applied mode.

因此,对于FileField,使用指定的模式打开重新打开文件。然后,一旦调用open,就可以继续使用诸如read之类的方法,使用新应用的模式。

#4


0  

Surprisingly but django.db.models.fields.files does not utilize file.storage.exists() method so I had to implement my own small function to have cross-storage compatible check for actual physical file existence:

但django.db.models.fields得惊人。文件不使用file.storage.exists()方法,因此我必须实现自己的小函数,以便对实际的物理文件存在进行跨存储兼容检查:

# Check whether actual file of FileField exists (is not deleted / moved out).
def file_exists(obj):
    return obj.storage.exists(obj.name)