2022-10

2022-10-04 Accepting Files

Episode Summary

Maybe your app needs to handle files from users like profile pictures .

Accepting files from others is tricky to do safely. On this episode, we’ll see the tools that Django provides to manage files safely.

Full show notes are available at https://www.mattlayman.com/django-riffs/17 .

Files In Django Models

While it is possible to store file data directly in a database, you won’t see that happen often.

The reason is that storing the data in the database usually affects the performance of the database, especially with a large number of files.

Instead, a common pattern in database usage is to store files separately from the database itself. Within the database, a column would store some kind of reference to the stored file like a path if files are stored on a filesystem.

This is the approach that Django takes with files .

Now that you know that Django takes this approach, you can remember:

Django models hold the reference to a file (e.g., a file path) The file data (i.e., the file itself) is stored somewhere else.

The “somewhere else” is called the “file storage,” and we’ll discuss storage in more depth in the next section.

Django includes two fields that help with file management:

Django-storage