32
loading...
This website collects cookies to deliver better user experience
FileField
ImageField
# application/models.py
from django.db import models
class Profile(models.Model):
picture = models.FileField()
# Other fields like a OneToOneKey to User ...
$ ./manage.py shell
>>> from django.core.files import File
>>> from application.models import Profile
>>> f = open('/Users/matt/path/to/image.png')
>>> profile = Profile()
>>> profile.picture.save('my-image.png', File(f))
File
class is an important wrapper that Django uses to make Python file objects (i.e., the value returned from open
) work with the storage system.image.png
and my-image.png
do not have to match. Django can store the content of image.png
and use my-image.png
as the name to reference within the storage system.my-image.png
"?upload_to
field keyword argument. The simplest version of upload_to
can take a string that storage will use as a directory prefix to scope content into a different area.upload_to
can also accept a callable that gives us a chance to fix that issue. Let's rework the example.# application/models.py
import uuid
from pathlib import Path
from django.db import models
def profile_pic_path(instance, filename):
path = Path(filename)
return "profile_pics/{}{}".format(uuid.uuid4(), path.suffix)
class Profile(models.Model):
picture = models.FileField(upload_to=profile_pic_path)
# Other fields like a OneToOneKey to User ...
profile_pics
path within the file storage.profile_pic_path
ignores most of the original filename provided. If two users both happen to upload profile-pic.jpg
, profile_pic_path
will assign those images random IDs and ignore the profile-pic
part of the filename.uuid4()
. These are effectively random IDs called Universally Unique Identifiers (UUID). UUIDs are likely something that you've seen before if you've worked with computers long enough, even if you didn't know their name. An example UUID would be 76ee4ae4-8659-4b50-a04f-e222df9a656a
. In the storage area, you might find a file stored as:profile_pics/76ee4ae4-8659-4b50-a04f-e222df9a656a.jpg
uuid4()
is nearly certain to generate a unique value. Because of this feature, we can avoid filename conflicts by storing profile pictures with a unique name.ImageField
has value. This field type contains extra validation logic that can check the content of the file to check that the file is, in fact, an image. To use ImageField
, you'll need to install the Pillow library. Pillow is a package that let's Python work with image data.# application/models.py
import uuid
from pathlib import Path
from django.db import models
def profile_pic_path(instance, filename):
path = Path(filename)
return "profile_pics/{}{}".format(uuid.uuid4(), path.suffix)
class Profile(models.Model):
picture = models.ImageField(upload_to=profile_pic_path)
# Other fields like a OneToOneKey to User ...
DEFAULT_FILE_STORAGE
. This setting is a Python module path string to the specific class.django.core.files.storage.FileSystemStorage
. The storage class uses a couple of important settings: MEDIA_ROOT
and MEDIA_URL
.MEDIA_ROOT
setting defines where Django should look for files in the filesystem.MEDIA_ROOT = BASE_DIR / "media"
Profile
class example from earlier, Django would store a file somewhere like:# This path is split to be easier to read.
/Users/matt/example-app/ \
media/profile_pics/76ee4ae4-8659-4b50-a04f-e222df9a656a.jpg
FileSystemStorage
is MEDIA_URL
. This settings will determine how files are accessed by browsers when Django is running. Let's say MEDIA_URL
is:MEDIA_URL = "/media/"
>>> from application.models import Profile
>>> profile = Profile.objects.last()
>>> profile.picture.url
'/media/profile_pics/76ee4ae4-8659-4b50-a04f-e222df9a656a.jpg'
<img src="{{ profile.picture.url }}">
FileSystemStorage
happens to be included with Django and implements this interface for the simplest storage mechanism, the file system of your server's operating system.FileSystemStorage
to store files for your application? There are actually many possible problems! Here are a few:FileSystemStorage
will not work for your app, is there another good option? Absolutely!DEFAULT_FILE_STORAGE
class. This setup includes setting AWS private keys, access controls, regions, buckets, and a handful of other important settings.FileSystemStorage
../manage.py
. You'll learn about: