liveBook · Manning

FileField

As explained in Django in Action

A FileField is a Django model field that stores a reference to an uploaded file and manages its file path.

A FileField is a Django model field for uploaded files. The file itself lives on the server, while the database stores a reference to its path. This keeps large file data out of the database while still associating each upload with application data. section 7.4 section 4.8.2

Using a FileField involves more than adding a model attribute: an upload form and view must receive the file, and the model must define where the resulting path is managed. Django also prevents filename collisions when saving uploads. section 7.4.2 section 7.4.2

A venue upload field

A Venue model can store an optional picture with `picture = models.FileField(blank=True, null=True)`. After adding the field, run `makemigrations` and `migrate`, then use Django Admin to edit a venue and upload a file. Django stores the file in the uploads directory and records its path with the venue. section 7.4.2

Choose the field according to what the upload represents:

  • Use FileField for general files, including SVG files that ImageField cannot store. section 7.4.2
  • Use ImageField when the upload must be validated as an image; it relies on Pillow and supports formats Pillow recognizes. section 7.4 section 7.4.2

How Django in Action covers it

How deep it goes
Explains storage, model changes, migrations, Admin uploads, collision handling, and path customization, but not full form-view implementation.
Examples
Demonstrates venue pictures, Django Admin uploads, date-based paths, and callable paths based on an owner.
What it assumes
Assumes basic Django models and database migrations, while introducing server-side file storage and upload associations.
What you can do after
Operate/configure Django uploads by adding a field, migrating its database column, and choosing a storage path.
How it connects
Connects FileField with Django models, database migrations, forms, views, Django Admin, upload paths, and ImageField.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.