管理文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/boyun58/article/details/89763307

默认情况下,Django使用MEDIA_ROOT和 MEDIA_URL设置在本地存储文件。
但是,Django提供了编写自定义文件存储系统的方法,允许您完全自定义Django存储文件的位置和方式。

#使用ImageField存储照片:
from django.db import models

class Car(models.Model):
    name = models.CharField(max_length=255)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    photo = models.ImageField(upload_to='cars')

#######################################################################
>>> car = Car.objects.get(name="57 Chevy")
>>> car.photo
<ImageFieldFile: chevy.jpg>
>>> car.photo.name
'cars/chevy.jpg'
>>> car.photo.path
'/media/cars/chevy.jpg'
>>> car.photo.url
'http://media.example.com/cars/chevy.jpg'

例如,可以通过将文件设置为name相对于文件存储位置的路径来更改文件名 (MEDIA_ROOT如果使用默认值 FileSystemStorage):

>>> import os
>>> from django.conf import settings
>>> initial_path = car.photo.path
>>> car.photo.name = 'cars/chevy_ii.jpg'
>>> new_path = settings.MEDIA_ROOT + car.photo.name
>>> # Move the file on the filesystem
>>> os.rename(initial_path, new_path)
>>> car.save()
>>> car.photo.path
'/media/cars/chevy_ii.jpg'
>>> car.photo.path == new_path
True

File对象

在内部,Django 在需要表示文件的任何时候都使用django.core.files.File实例。

大多数情况下,你只需使用Django给你的FIle(即上面附带模型的文件,或者上传的文件)。

如果你需要构建一个File,最简单的方法是使用Python内置file对象创建一个:

>>> from django.core.files import File

# Create a Python file object using open()
>>> f = open('/path/to/hello.world', 'w')
>>> myfile = File(f)

现在可以使用File该类的任何记录的属性和方法。

请注意,以这种方式创建的文件不会自动关闭。以下方法可用于自动关闭文件:
>>> from django.core.files import File
# Create a Python file object using open() and the with statement
>>> with open('/path/to/hello.world', 'w') as f:
...     myfile = File(f)
...     myfile.write('Hello World')
...
>>> myfile.closed
True
>>> f.closed
True

在通过大量对象循环访问文件字段时,关闭文件尤为重要。如果在访问文件后未手动关闭文件,则可能会出现文件描述符用完的风险。这可能会导致以下错误:
IOError: [Errno 24] Too many open files

文件存储

在幕后,Django委托决定如何以及在何处将文件存储到文件存储系统。这是实际理解文件系统,打开和读取文件等内容的对象。

Django的默认文件存储由DEFAULT_FILE_STORAGE 设置给出; 如果您没有明确提供存储系统,则将使用该存储系统。

存储对象

虽然大多数情况下您都希望使用File对象(委托给该文件的正确存储),但您可以直接使用文件存储系统。您可以创建某个自定义文件存储类的实例,或者 - 您可以使用全局默认存储系统 - 通常更有用:

>>> from django.core.files.base import ContentFile
>>> from django.core.files.storage import default_storage

>>> path = default_storage.save('/path/to/file', ContentFile('new content'))
>>> path
'/path/to/file'

>>> default_storage.size(path)
11
>>> default_storage.open(path).read()
'new content'

>>> default_storage.delete(path)
>>> default_storage.exists(path)
False

内置文件系统存储类

Django附带了一个django.core.files.storage.FileSystemStorage实现基本本地文件系统文件存储的类。

例如,以下代码将存储上传的文件, 无论您的MEDIA_ROOT设置是什么:

from django.core.files.storage import FileSystemStorage
from django.db import models

fs = FileSystemStorage(location='/media/photos')

class Car(models.Model):
    ...
    photo = models.ImageField(storage=fs)

自定义存储系统的工作方式相同:您可以将它们作为storage参数传递给FileField。

猜你喜欢

转载自blog.csdn.net/boyun58/article/details/89763307